android 修改AlertDialog的黑色背景的两种方式及圆角边框的设置

修改黑色背景

第一种方式

直接使用AlertDialog.Builder的方法

AlertDialog dialog = new AlertDialog.Builder(this)
                    .create();
dialog.setInverseBackgroundForced(true);// 背景变成了白色

第二种方式

自定义一个布局,在dialog显示之后,重新设置dialog的内容view

AlertDialog dialog = new AlertDialog.Builder(this)
                    .create();
dialog.show();
Window window = dialog.getWindow();
window.setContentView(R.layout.custom_alert_dialog);
// 设置背景透明
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

注意:一定要在show()方法之后重新设置布局。至于为什么,实践出真知
在这里插入图片描述

AlertDialog的圆角边框

设置修改Alertdialog黑色背景的第二种方式的布局文件R.layout.custom_alert_dialog的背景为圆角边框即可
下面是布局及圆角的代码
R.layout.custom_alert_dialog.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="wrap_content"
    android:background="@drawable/bg_dialog"
    android:paddingRight="20dp"
    android:paddingLeft="20dp"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="编辑"/>
    <EditText
        android:id="@+id/et_countDownWord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_weight="1"
            android:text="取消"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_weight="1"
            android:text="确定"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

圆角,bg_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp"/>
    <!--设置背景色-->
    <solid android:color="#ffffff"/>
</shape>

修改自https://www.cnblogs.com/yaowukonga/archive/2012/05/19/2508743.html

https://blog.csdn.net/csdnzouqi/article/details/84337004

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值