sd.setOnClickListener(new View.OnClickListener() { private PopupWindow popupWindow; @Override public void onClick(View view) { View inflate = View.inflate(MainActivity.this, R.layout.popupwindow, null); popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //必须设置 popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent))); popupWindow.setOutsideTouchable(true); //显示在底部 main为activity_main布局控件中 最大的LinearLayout 的id popupWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); Button bt = inflate.findViewById(R.id.bt); Button bt1 = inflate.findViewById(R.id.bt1); Button bt2 = inflate.findViewById(R.id.bt2); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoObtainCameraPermission(); popupWindow.dismiss(); } }); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoObtainStoragePermission(); popupWindow.dismiss(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); } }); .xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:background="#ffffff" android:layout_gravity="center_horizontal" android:text="拍照" android:id="@+id/bt" android:layout_width="match_parent" android:layout_height="wrap_content" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#DFDCD7"></View> <Button android:background="#ffffff" android:layout_gravity="center_horizontal" android:text="从相册选取" android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:background="#ffffff" android:layout_gravity="center_horizontal" android:text="取消" android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
popuwindow
最新推荐文章于 2024-06-22 00:03:12 发布