版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
效果如下图,点击按钮弹出对话框,背景颜色是半透明
一、PopupWindow的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/btn_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/function_feedback_bottom_dialog_shape_album"
android:gravity="center"
android:padding="15dp"
android:text="拍照"
android:textColor="#1079FF"
android:textSize="18sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<TextView
android:id="@+id/btn_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/function_feedback_bottom_dialog_shape_camera"
android:gravity="center"
android:padding="15dp"
android:text="相册"
android:textColor="#1079FF"
android:textSize="18sp" />
<TextView
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/function_feedback_bottom_dialog_shape_cancel"
android:gravity="center"
android:padding="15dp"
android:text="取消"
android:textColor="#1079FF"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
二、三个TextView的 android:background对应的文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
<solid android:color="@color/white"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"/>
<solid android:color="@color/white"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid android:color="@color/white"/>
</shape>
三、创建PopupWindow
//解析布局文件
View bottomView = View.inflate(context, R.layout.layout_funtion_feedback_dialog, null);
/**
* 打开PopupWindow弹窗
*/
private void showPop() {
mPopupWindow = new PopupWindow(bottomView, -1, -2);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(true);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.5f;
getWindow().setAttributes(lp);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
mPopupWindow.setAnimationStyle(R.style.DialogWindowStyle);
mPopupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
}