Android 自定义AlertDialog 并且4个角为圆角

本文将介绍如何在Android中创建一个自定义的AlertDialog,使其四个角落呈现圆形效果,详细讲解了实现圆角对话框的步骤和技术要点。
//自定义AlertDialog
LayoutInflater inflater = getLayoutInflater();
View view1 = inflater.inflate(R.layout.dialog_activity, null);
ImageView ivguanbi = view1.findViewById(R.id.iv_guanbi);
TextView wjmm = view1.findViewById(R.id.tv_wjmm);
EditText namephone = view1.findViewById(R.id.et_namephone);
EditText newpassword = view1.findViewById(R.id.et_newpassword);
EditText yanzhenma = view1.findViewById(R.id.yanzhenma);
Button cxhqyzm = view1.findViewById(R.id.btn_nxhqyzm);
//给textview字体加粗
TextPaint paint = wjmm.getPaint();
paint.setFakeBoldText(true);
显示AlertDialogAlertDialog dialog = new AlertDialog.Builder(this)
        .setView(view1)
        .show();
//给AlertDialog设置4个圆角
dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbg);

设置dialogbg或者说设置圆角并填充背景色或者说设置shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="30dp" />

    <solid android:color="@color/colorWhite" />

</shape>

AlertDialog自定义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:background="@drawable/dialogbg"
    android:orientation="vertical"
    android:padding="30dp">

    <ImageView
        android:id="@+id/iv_guanbi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@mipmap/btn_close" />

    <TextView
        android:id="@+id/tv_wjmm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="忘记密码"
        android:textColor="@color/colorBlack"
        android:textSize="25sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="用户名:"
        android:textSize="12sp" />

    <EditText
        android:id="@+id/et_namephone"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/list_insert"
        android:gravity="center"
        android:hint="注册所用手机号" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="新密码:"
        android:textSize="12sp" />

    <EditText
        android:id="@+id/et_newpassword"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/list_insert"
        android:gravity="center"
        android:inputType="textPassword" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="验证码:"
        android:textSize="12sp" />

    <EditText
        android:id="@+id/yanzhenma"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/list_insert"
        android:gravity="center" />

    <Button
        android:id="@+id/btn_nxhqyzm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg"
        android:text="从新或取验证码"
        android:textColor="@color/colorWhite" />
</LinearLayout>
Android 中为 AlertDialog 自定义圆角背景,可参考以下几种方法: #### 方法一:消除白块并设置自定义布局 创建 AlertDialog 后,通过设置窗口背景为 `BitmapDrawable` 消除白块,再设置自定义布局。示例代码如下: ```java AlertDialog alertDialog = new AlertDialog.Builder(this).create(); Window window = alertDialog.getWindow(); // 消除白块 window.setBackgroundDrawable(new BitmapDrawable()); // 自定义的 xml window.setContentView(R.layout.mydialog); alertDialog.show(); ``` 同时,在 `res/drawable` 目录下创建圆角背景的 XML 文件,如 `dialog_bg.xml`: ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/mywhite" /> <!-- 背景色 --> <corners android:topLeftRadius="15dp" android:topRightRadius="15dp" android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp"/> <!-- 四个圆角圆角大小 --> <stroke android:width="1dp" android:color="@color/mywhite" /> <!-- 边框色 --> </shape> ``` 将该背景应用到自定义布局的根视图中,在 `mydialog.xml` 里设置 `android:background="@drawable/dialog_bg"` [^2]。 #### 方法二:设置背景为透明色 将 Dialog 的背景设置为透明色,代码如下: ```java getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); ``` 之后再设置自定义圆角背景 [^3]。 #### 方法三:使用 `setBackgroundDrawableResource` 方法 自定义 AlertDialog 布局,然后显示 AlertDialog 并设置圆角背景。示例代码如下: ```java // 自定义 AlertDialog LayoutInflater inflater = getLayoutInflater(); View view1 = inflater.inflate(R.layout.dialog_activity, null); // 查找视图控件 ImageView ivguanbi = view1.findViewById(R.id.iv_guanbi); TextView wjmm = view1.findViewById(R.id.tv_wjmm); EditText namephone = view1.findViewById(R.id.et_namephone); EditText newpassword = view1.findViewById(R.id.et_newpassword); EditText yanzhenma = view1.findViewById(R.id.yanzhenma); Button cxhqyzm = view1.findViewById(R.id.btn_nxhqyzm); // 给 textview 字体加粗 TextPaint paint = wjmm.getPaint(); paint.setFakeBoldText(true); // 显示 AlertDialog AlertDialog dialog = new AlertDialog.Builder(this) .setView(view1) .show(); // 给 AlertDialog 设置 4圆角 dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbg); ``` 在 `res/drawable` 目录下创建 `dialogbg.xml` 文件: ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="30dp" /> <solid android:color="@color/colorWhite" /> </shape> ``` 这里的 `@color/colorWhite` 是自定义的白色 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值