PopupWindow
效果图
代码
layout_pop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/timg"
>
<TextView
android:id="@+id/tv_good"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/colorGray"
android:text="好"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
/>
<TextView
android:id="@+id/tv_good2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/colorGray"
android:text="还行"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
/>
<TextView
android:id="@+id/tv_bad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/colorGray"
android:text="不好"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
/>
</LinearLayout>
PopupWindowActivity
public class PopupWindowActivity extends AppCompatActivity {
private Button mBtnPop;
private PopupWindow mPop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window);
mBtnPop = findViewById(R.id.btn_pop);
mBtnPop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.layout_pop, null);
TextView tv = view.findViewById(R.id.tv_good);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPop.dismiss();
ToastUtil.showLong(PopupWindowActivity.this, ((TextView)v).getText());
}
});
mPop = new PopupWindow(view, mBtnPop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
mPop.setOutsideTouchable(false);
mPop.setFocusable(true);
mPop.showAsDropDown(mBtnPop);
}
});
}
}