简单快速取消AlertDialog的白色背景框,AlertDialog设置圆角背景

问题描述:

产品需求弹出的提示框是圆角,使用shape 设置圆角背景后,弹出的AlertDialog提示框四个角有白色的背景,据分析这个背景是 AlertDialog 父组件的背景色。

解决方法:

将Dialog的背景设置为透明色,代码如下:

java 复制代码
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

详细将AlertDialog设置圆角背景方法:

1、首先在drawable文件夹下建一个xml文件,如ic_background_radius.xml

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/grey_c4c4c4"/>  //填充色
    <corners android:radius="16dp"/>  //圆角大小
</shape>

2、然后在layout文件夹下建一个xml布局文件,如dialog_face_compare_layout.xml 。在布局文件的根布局中设置了background属性为@drawable/ic_background_radius,目的是为了让dialog显示圆角边框。

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/ic_background_radius"   //设置定义的圆角背景
    android:padding="16dp">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp">

        <ImageView
            android:id="@+id/iv_face_compare"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerInParent="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_face_comparing2" />

        <ImageView
            android:id="@+id/iv_scan_line"
            android:layout_width="180dp"
            android:layout_height="3dp"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_line_drawable" />

    </RelativeLayout>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="请正视前方"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

3、在MainAcitvity中写弹出AlertDialog的方法,此处用的dialog是引用外部布局的方式,要想去掉四角的白色背景框至关重要的一句是:

tipDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

java 复制代码
View tipInflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_face_compare_layout, null);  //设置dialog显示的布局
        AlertDialog tipDialog = new AlertDialog.Builder(mContext).setView(tipInflate).create();//创建AlertDialog对象
        tipDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//将AlertDialog父组件的背景设置为透明色
        tipDialog.show();  //显示提示框

修改后效果图:

相关推荐
cui_hao_nan3 分钟前
设计模式——模板方法
java·设计模式
小吕学编程3 分钟前
HttpServletRequest常用方法
java·http
在未来等你4 分钟前
Java并发编程实战 Day 11:并发设计模式
java·设计模式·多线程·并发编程·threadlocal·生产者消费者·读写锁
李少兄16 分钟前
解决 idea提示`SQL dialect is not configured` 问题
java·sql·intellij-idea
BreezeDove21 分钟前
IDEA安装&迁移IDEA配置数据位置
java·ide·intellij-idea
太阳之神aboluo26 分钟前
压测软件-Jmeter
java·运维·jmeter
编程乐学(Arfan开发工程师)39 分钟前
42、响应处理-【源码分析】-浏览器与PostMan内容协商完全适配
java·spring boot·后端·测试工具·lua·postman
珹洺44 分钟前
数据库系统概论(十七)超详细讲解数据库规范化与五大范式(从函数依赖到多值依赖,再到五大范式,附带例题,表格,知识图谱对比带你一步步掌握)
java·数据库·sql·安全·oracle
用户79117724235831 小时前
黑马点评【基于redis实现共享session登录】
java·redis
网安INF1 小时前
CVE-2023-25194源码分析与漏洞复现(Kafka JNDI注入)
java·web安全·网络安全·kafka·漏洞·jndi注入