Android自定义AlertDialog点击按钮不消失且底部显白背景问题
解决自定义AlertDialog按钮无法关闭及白色背景问题
嘿,我来帮你搞定这两个问题!咱们一个个来解决:
一、按钮无法关闭弹窗的问题
你的代码逻辑其实没大问题,但有时候直接调用new AlertDialog.Builder(this).create()再设置view,可能会导致Dialog对象的关联出现小问题。咱们调整一下构建AlertDialog的方式,确保点击事件能正确触发关闭:
修改后的showCustomAlertDialog()方法:
public void showCustomAlertDialog() { LayoutInflater layoutInflater = LayoutInflater.from(this); final View promptView = layoutInflater.inflate(R.layout.reminder_alert_dialog, null); // 先通过Builder构建,再创建AlertDialog实例 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(promptView); final AlertDialog alertDialog = builder.create(); Button recordButton = (Button) promptView.findViewById(R.id.record_button); recordButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { System.out.println("Dismiss the dialog"); alertDialog.dismiss(); // 调用当前显示的Dialog实例的关闭方法 } }); // 顺便解决白色背景问题,往下看 alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); alertDialog.show(); }
这样调整后,点击按钮就能正常关闭弹窗了,因为我们确保了dismiss()调用的是当前显示的那个AlertDialog实例。
二、额外白色背景的问题
这个是因为AlertDialog默认自带了白色的窗口背景,和你的自定义布局叠加后就会出现多余的白色区域。只需要在显示Dialog前,把默认的窗口背景设为透明即可,就是上面代码里的这一行:
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
额外的布局优化建议
看了你的布局文件,还有几个小细节可以优化,让弹窗更美观:
- 把ImageView的
android:background改成android:src,这样图片会保持原有比例,不会被强制拉伸变形; - 调整控件的margin值,避免过大的间距导致弹窗过高(比如原布局里
200dp的margin可以缩小); - 给根布局增加
padding,让内容不会和弹窗边缘紧贴。
优化后的布局示例:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp"> <ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_alert" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/greetings_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GOOD MORNING, KEVIN" android:textColor="#000000" android:textSize="20sp" android:textStyle="bold" android:layout_marginTop="20dp" android:layout_below="@id/image_view" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/medicines_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Have you taken your medicines today?" android:textColor="#000000" android:textSize="15sp" android:textStyle="normal" android:layout_below="@id/greetings_text" android:layout_marginTop="15dp" android:layout_centerHorizontal="true"/> <Button android:id="@+id/record_button" android:layout_width="160dp" android:layout_height="40dp" android:background="#3BC4B8" android:text="Record" android:textSize="20sp" android:textColor="#ffffff" android:layout_below="@id/medicines_text" android:layout_marginTop="25dp" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp"/> </RelativeLayout>
按照上面的修改,你的自定义AlertDialog应该就能正常关闭,且不会有多余的白色背景啦!
内容的提问来源于stack exchange,提问作者Kunduri Raj
相关产品推荐
相关产品推荐

