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

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

一行解决:

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/304611.html

相关文章:

  • 使用 PyCharm 新建 Python 项目详解
  • turbo译码算法MAX, MAX_SCALE and MAX_STAR的比较
  • C 语言中表示对象大小的标准数据类型size_t
  • 备份服务器数据防护策略方法
  • 基于SpringBoot的在线汽车租赁信息管理系统
  • 【Java面试】第十一天
  • 九、外观模式
  • powerbi
  • QT中显示中文的几种方法及注意事项
  • JAVA毕业设计170—基于Java+Springboot+vue3+小程序的房屋租赁小程序系统(源代码+数据库)
  • 【STM32】DMA数据转运(存储器到外设)
  • VS Code 配置 Rust-Analyzer 报错
  • VUE下拉选择分页,远程搜索
  • 软件测试工程师面试整理-测试方法
  • ZCC40036 过压保护芯片
  • 用SpringBoot进行阿里云大模型接口调用同步方法和异步方法
  • C# 中的多线程同步:原子变量、原子操作、内存顺序和可见性
  • uni-app 聊天界面滚动到消息底部
  • Java后端程序员简单操作Linux系统命令
  • 深度学习——数据预处理,张量降维
  • Qt 边框border - qss样式
  • C++类与对象深度解析(一):从抽象到实践的全面入门指南
  • Linux相关:在阿里云下载centos系统镜像
  • Leetcode Hot 100刷题记录 -Day16(旋转图像)
  • 15.2 定义一个prometheus数据存储使用的pv
  • A Single Generic Prompt forSegmenting Camouflaged Objects
  • java: 程序包org.junit.jupiter.api不存在
  • 数据分析-前期数据处理
  • MacOS Sonoma(14.x) 大写模式或中文输入法下的英文模式,光标下方永远会出现的CapsLock箭头Icon的去除办法
  • Prompt提示词技巧