当前位置: 首页 > article >正文

Android AlertDialog圆角背景不生效的问题

一行解决:

window?.setBackgroundDrawableResource(android.R.color.transparent)

原文件:

/**
 * Created by Xinghai.Zhao
 * 自定义选择弹框
 */
@SuppressLint("InflateParams", "MissingInflatedId")
class CustomDialog(context: Context?) : AlertDialog(context){
    var mCallBack: ClickCallBack? = null
    var mTextViewTitle: TextView? = null
    var mTextViewContent: TextView? = null
    var mEditText:EditText? = null
    var mEditTextMax:TextView? = null
    var mEditLayout: View? = null
    var mTextViewYes: TextView? = null
    var mTextViewNo: TextView? = null
    var mLine: View? = null
    var isEdit:Boolean = false
    constructor(context: Context?, title: String?, content: String?, callBack: ClickCallBack, isEditType:Boolean) : this(context) {
        mCallBack = callBack
        isEdit = isEditType
        mTextViewTitle?.text = title?:""
        mTextViewContent?.text = content?:""
        if (isEdit){
            mEditLayout?.visibility = View.VISIBLE
        }else{
            mEditLayout?.visibility = View.GONE
        }
    }
    init {
        val inflate = LayoutInflater.from(context).inflate(R.layout.dialog_custom, null)
        setView(inflate)
        window?.setBackgroundDrawableResource(android.R.color.transparent)
        //设置点击别的区域不关闭页面
        setCancelable(false)
        mTextViewTitle = inflate.findViewById(R.id.dialog_custom_title)
        mTextViewContent = inflate.findViewById(R.id.dialog_custom_content)
        mEditText = inflate.findViewById(R.id.dialog_custom_edit)
        mEditTextMax = inflate.findViewById(R.id.dialog_custom_edit_max)
        mEditLayout = inflate.findViewById(R.id.dialog_custom_edit_layout)
        mTextViewYes = inflate.findViewById(R.id.dialog_custom_yes)
        mTextViewYes?.setOnClickListener{mCallBack?.onYesClick(this)}
        mTextViewNo = inflate.findViewById(R.id.dialog_custom_no)
        mTextViewNo?.setOnClickListener{dismiss()}
        mLine = inflate.findViewById(R.id.dialog_custom_line)
    }
    interface ClickCallBack {
        fun onYesClick(dialog: CustomDialog)
    }
}

layout:

<?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_set"
    android:orientation="vertical">
    <TextView
        android:id="@+id/dialog_custom_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold" />
   
    <TextView
        android:id="@+id/dialog_custom_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="30dp"
        android:textColor="@color/black"
        android:textSize="14sp" />
    <LinearLayout
        android:id="@+id/dialog_custom_edit_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="20dp"
        android:visibility="gone">

        <EditText
            android:id="@+id/dialog_custom_edit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_weight="1"
            android:maxLength="10"
            android:maxLines="1"
            android:padding="10dp"
            android:textColor="@color/black"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/dialog_custom_edit_max"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:padding="10dp"
            android:textColor="@color/grey"
            android:textSize="12sp" />


    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/grey2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/dialog_custom_no"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="取消"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/dialog_custom_line"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/grey2" />

        <TextView
            android:id="@+id/dialog_custom_yes"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="确定"
            android:textColor="@color/blue"
            android:textSize="16sp" />


    </LinearLayout>
</LinearLayout>

bg_set.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="14dp" /> <!-- 圆角半径为10dp -->
    <solid android:color="@color/white" /> <!-- 设置黑色背景并且70%不透明度 -->
</shape>


http://www.kler.cn/news/311128.html

相关文章:

  • Mybatis 和 数据库连接
  • Redis搭建集群
  • 如何更换OpenHarmony SDK API 10
  • 前端项目使用js将dom生成图片、PDF
  • Linux安装、Nginx反向代理、负载均衡学习
  • 95. UE5 GAS RPG 实现创建多段飞弹攻击敌人
  • C语言——自定义类型
  • Nginx 实现七层的负载均衡
  • 4位整数的数位和
  • OJ在线评测系统 前端开发设计优化通用菜单组件二 调试用户自动登录
  • 面试官:什么是CAS?存在什么问题?
  • 探索RESTful风格的网络请求:构建高效、可维护的API接口【后端 20】
  • AI换脸等违法行为的最关键原因是个人隐私信息的泄露,避免在网络上发布包含个人敏感信息的照片。
  • 图书管理系统(面向对象的编程练习)
  • 高级c语言(一)
  • Mybatis续
  • 36.贪心算法3
  • Android 内置应用裁剪
  • Java集合面试(上)
  • Kafka+PostgreSql,构建一个总线服务
  • k8s 微服务 ingress-nginx 金丝雀发布
  • ESRGAN——老旧照片、视频帧的修复和增强,提高图像的分辨率
  • ozon买家网址是什么,跨境电商ozon买家网址
  • YOLOv8的GPU环境搭建方法
  • 一个基于 laravel 和 amis 开发的后台框架, 友好的组件使用体验,可轻松实现复杂页面(附源码)
  • 一对一,表的设计
  • Nginx中return和rewrite的区别
  • python 实现entropy熵算法
  • c++ static(详解)
  • Snowflake怎么用?