Android 让Dialog实现背景透明
2019-08-13 本文已影响0人
Andr0
1、实现自定义Dialog 布局 dialog_main_sign.xml
<?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="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/ic_main_sign_pop" />
<ImageView
android:id="@+id/img_sing_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20"
android:src="@drawable/ic_main_sign_x" />
</LinearLayout>
2、代码
private ImageView img_sing_x;
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(me.getContext());
final AlertDialog dialog = builder.create();
View dialogView = View.inflate(this.getContext(), R.layout.dialog_main_sign, null);
dialog.setView(dialogView, 0, 0, 0, 0); // 去边距,兼容2.X
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
img_sing_x = (ImageView) dialogView.findViewById(R.id.img_sing_x); // guanbi
dialogView.findViewById(R.id.img_sing_x).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
注意: dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
这才是实现背景透明的重点