Kotlin中自定义dialog

article/2025/11/6 4:36:22

文章目录

    • 效果
    • dialog样式
    • dialog 背景 common_dialog_bg
    • dialog布局 base_common_dialog_layout
    • 全局工具类 CommonDialog(建造者模式)
    • 使用

效果

在这里插入图片描述

dialog样式

   <style name="custom_dialog2" parent="@android:style/Theme.Dialog"><item name="android:windowFrame">@null</item><!-- Dialog的windowFrame框为无 --><item name="android:windowIsFloating">true</item><!-- 是否漂现在activity上 --><item name="android:windowIsTranslucent">true</item><!-- 是否半透明 --><item name="android:windowNoTitle">true</item><item name="android:background">@null</item><item name="android:windowBackground">@android:color/transparent</item><item name="android:windowContentOverlay">@null</item><!-- 去除黑色边框的关键设置项 --><item name="android:backgroundDimEnabled">true</item><!-- 屏幕背景是否变暗 --><item name="android:backgroundDimAmount">0.3</item></style>

dialog 背景 common_dialog_bg

<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/white" /><corners android:radius="15dp" />
</shape>

dialog布局 base_common_dialog_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="match_parent"android:background="@drawable/common_dialog_bg"android:clipChildren="false"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="145dp"android:layout_gravity="center"android:orientation="vertical"><TextViewandroid:id="@+id/tv_dialog_title"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3"android:padding="5dp"android:gravity="center"android:text="推荐*********体验!"android:textColor="#666666"android:textSize="14sp" /><ImageViewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@color/background_grey" /><!--取消确认--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_alignParentBottom="true"android:layout_weight="2"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_dialog_pos"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="直接打开"android:textColor="@color/blue"android:textSize="16sp" /><ImageViewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="@color/background_grey" /><TextViewandroid:id="@+id/tv_dialog_neg"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="导入应用打开"android:textColor="@color/blue"android:textSize="16sp" /></LinearLayout></LinearLayout></LinearLayout>

全局工具类 CommonDialog(建造者模式)


import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import android.view.*
import android.widget.TextView
import com.beans.base.R/*** @Author yangtianfu* @CreateTime 2020/4/7 11:16* @Describe 通用弹窗*/class CommonDialog(context: Context?, themeResId: Int) : Dialog(context!!, themeResId) {class Builder (private val context: Context) {private var title: String? = nullprivate var message: String? = nullprivate var positiveButtonContent: String? = nullprivate var negativeButtonContent: String? = nullprivate var positiveButtonListener: DialogInterface.OnClickListener? = nullprivate var negativeButtonListener: DialogInterface.OnClickListener? = nullprivate var contentView: View? = nullprivate var color: Int = 0private var withOffSize: Float = 0.toFloat()private var heightOffSize: Float = 0.toFloat()fun setTitle(title: String): Builder {this.title = titlereturn this}fun setTitle(title: Int): Builder {this.title = context.getText(title) as Stringreturn this}fun setMessage(message: String): Builder {this.message = messagereturn this}fun setMessageColor(color: Int): Builder {this.color = colorreturn this}fun setPositiveButton(text: String, listener: DialogInterface.OnClickListener): Builder {this.positiveButtonContent = textthis.positiveButtonListener = listenerreturn this}fun setPositiveButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {this.positiveButtonContent = context.getText(textId) as Stringthis.positiveButtonListener = listenerreturn this}fun setNegativeButton(text: String, listener: DialogInterface.OnClickListener): Builder {this.negativeButtonContent = textthis.negativeButtonListener = listenerreturn this}fun setNegativeButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {this.negativeButtonContent = context.getText(textId) as Stringthis.negativeButtonListener = listenerreturn this}fun setContentView(v: View): Builder {this.contentView = vreturn this}fun setWith(v: Float): Builder {this.withOffSize = vreturn this}fun setContentView(v: Float): Builder {this.heightOffSize = vreturn this}fun create(): CommonDialog {/*** 初始化Dialog*/val dialog = CommonDialog(context,R.style.custom_dialog2)/*** 初始化Dialog的布局页面*/val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflaterval dialogLayoutView = inflater.inflate(R.layout.base_common_dialog_layout,null)dialog.addContentView(dialogLayoutView, ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))if (!TextUtils.isEmpty(title)) {(dialogLayoutView.findViewById<View>(R.id.tv_dialog_title) as TextView).text = title}if (color != 0) {val viewById = dialogLayoutView.findViewById<View>(R.id.tv_dialog_title) as TextViewviewById.setTextColor(color)}if (!TextUtils.isEmpty(message)) {(dialogLayoutView.findViewById<View>(R.id.tv_dialog_title) as TextView).text = message}if (!TextUtils.isEmpty(positiveButtonContent)) {(dialogLayoutView.findViewById<View>(R.id.tv_dialog_pos) as TextView).text = positiveButtonContentif (positiveButtonListener != null) {(dialog.findViewById<View>(R.id.tv_dialog_pos) as TextView).setOnClickListener { positiveButtonListener!!.onClick(dialog, -1) }}} else {(dialogLayoutView.findViewById<View>(R.id.tv_dialog_pos) as TextView).visibility = View.GONEdialogLayoutView.findViewById<View>(R.id.line).visibility = View.GONE}if (!TextUtils.isEmpty(negativeButtonContent)) {(dialogLayoutView.findViewById<View>(R.id.tv_dialog_neg) as TextView).text = negativeButtonContentif (negativeButtonListener != null) {(dialogLayoutView.findViewById<View>(R.id.tv_dialog_neg) as TextView).setOnClickListener { negativeButtonListener!!.onClick(dialog, -2) }}} else {(dialogLayoutView.findViewById<View>(R.id.tv_dialog_neg) as TextView).visibility = View.GONE}/*** 将初始化完整的布局添加到dialog中*/dialog.setContentView(dialogLayoutView)/*** 禁止点击Dialog以外的区域时Dialog消失*/dialog.setCanceledOnTouchOutside(false)val window = dialog.windowval context = this.context as Activityval windowManager = context.windowManagerval defaultDisplay = windowManager.defaultDisplayval attributes = window!!.attributesif (withOffSize.toDouble() != 0.0) {attributes.width = (defaultDisplay.width * withOffSize).toInt()} else {attributes.width = (defaultDisplay.width * 0.77).toInt()}if (heightOffSize.toDouble() != 0.0) {attributes.height = (defaultDisplay.height * heightOffSize).toInt()}window.attributes = attributesreturn dialog}}
}

使用

                CommonDialog.Builder(this).setMessage("推荐导入体验!").setNegativeButton("导入应用打开",DialogInterface.OnClickListener{p0,p1 -> p0.dismiss()}).setMessageColor(Color.BLACK).setPositiveButton("直接打开",DialogInterface.OnClickListener({ dialog, which ->  dialog.dismiss()})).setWith(0.8f).create().show()

http://chatgpt.dhexx.cn/article/EOUKH0kZ.shtml

相关文章

Android 简单的自定义Dialog

效果图 Dialog的详细用法看这篇博客&#xff1a;http://blog.csdn.net/zhuwentao2150/article/details/51478053 我们自定义的CustomDialog是通过继承Dialog类并添加自定义的布局来实现的 /*** 自定义Dialog弹窗* Created by zhuwentao on 2016-08-19.*/ public class Cus…

实现自定义dialog样式

1定义弹出的dialog样式 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orientation"vertical"android:layout_width"match_parent"a…

Android自定义Dialog+圆角处理

目录 一、自定义Dialog 二、Dialog添加圆角 一、自定义Dialog 自定义Dialog实现过程 本文的自定义dialog是在fragment中实现的&#xff0c;在Activity里面应该大同小异了。 android studio其实自带了很多种dialog 下面这个图是引用一位大佬的&#xff01; 这几天在学习自定…

Android 自定义Dialog实现(二)

在之前的文章中介绍了自定Dialog的实现方法之一&#xff1a;https://blog.csdn.net/m0_57487205/article/details/124775019?spm1001.2014.3001.5501https://blog.csdn.net/m0_57487205/article/details/124775019?spm1001.2014.3001.5501 这篇文章记录一下另外一种实现…

Android-自定义Dialog

Android-自定义Dialog 2014年4月27日 星期天 天气晴朗 心情平静 本篇博文来分享一个也是开发中经常需要用到的功能-自定义对话框&#xff0c;这里我用到了Android中的图形资源shape&#xff0c;具体使用方法&#xff0c;各位看代码吧&#xff0c;Android有多钟图形资源&#xf…

Flutter 自定义Dialog

我们项目开发中&#xff0c;有很多地方会用到dialog&#xff0c;虽然flutter自身也有&#xff0c;比如AboutDialog、AlertDialog、SimpleDialog、CupertinoAlertDialog等等之类的&#xff0c;但是这些满足不了我们的控制欲&#xff0c;我们想要的是它可以根据我们的想法而随改变…

安卓自定义dialog弹窗

1.先设置dialog样式&#xff0c;style.xml <!-- dialog样式 --><style name"DialogTheme" parent"android:style/Theme.Dialog"><!-- 边框 --><item name"android:windowFrame">null</item><!-- 是否浮现在act…

Android 自定义Dialog的实现

最新实现了一个自定义Dialog的需求&#xff0c;先看看效果图&#xff1a; 下面说说如何实现&#xff1a; 首先需要自定义一个Dialog类&#xff0c;继承自android.app.Dialog类。这个Dialog类就是要显示的对话框&#xff0c;包含双选按钮和单选按钮两种效果。本例中自定义Custo…

Android自定义Dialog对话框的几种方法(精简版)

自定义对话框是经常使用的功能&#xff0c;我们常用的弹窗操作&#xff0c;除了使用popwindow就是使用dialog来实现&#xff0c;这两种组件都支持之定义布局和功能来满足我们个性化的需求&#xff0c;也可以不采用自定义而直接使用系统封装好的api来实现功能。今天简单总结下在…

自定义Dialog的简单实现

自定义Dialog的详细步骤&#xff08;实现自定义样式一般原理&#xff09; 发表于2016/3/22 22:12:57 1410人阅读 分类&#xff1a; android开发 转载请标注转载http://blog.csdn.net/oqihaogongyuan/article/details/50958659 自定义Dialog的详细步骤&#xff08;实现自定义样…

安卓dialog的使用+如何自定义dialog

吐槽 哇哇哇&#xff0c;刚写一半win10给我蓝屏了&#xff0c;心塞塞&#xff0c;以后写一点保存一点。回到正题&#xff0c;看到产品给我的设计图&#xff0c;有辣么多的自定义的dialog&#xff0c;发现之前自己只会系统自带的dialog&#xff0c;但是这样根本满足不了产品的需…

Android/安卓 自定义Dialog 最简单、最详细解释

看了很多视频&#xff0c;也在网上找了一些浏览量最多的文章&#xff0c;发现都太难懂或者太复杂&#xff0c;夹杂了很多其他功能&#xff0c;自定义度太高&#xff0c;很繁琐。所以我想写一个基础的自定义Dialog&#xff0c;只涉及基础的自定义&#xff0c;其他复杂的自定义可…

自定义Dialog的详细步骤(实现自定义样式一般原理)

转载请标注转载http://blog.csdn.net/oqihaogongyuan/article/details/50958659 自定义Dialog的详细步骤&#xff08;实现自定义样式一般原理&#xff09; 现在很多App的提示对话框都非常有个性&#xff0c;然而你还用系统的对话框样式&#xff0c;是不是觉得很落后呢&#xf…

CSS 文字超出部分显示省略号

一&#xff1a; title { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 以上 CSS 适用于单行文字超出部分&#xff0c;显示「省略号」。如图&#xff1a; 二&#xff1a; title { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-cl…

css三行代码实现多行文字超出部分省略号代替

就是我们在页面开发的时候有时为了更加美观&#xff0c;让文字在同行显示&#xff0c;超出的部分用省略号替代 而这个效果我们直接实现3行css实现 white-space: nowrap; 让文字在一行显示&#xff0c;不允许换行 overflow: hidden; 超出部分隐藏 text-overflow: ellipsis; 超…

IOS不兼容超出部分省略号 且页面显示不起作用 行数限定无作用

IOS不兼容超出部分省略号 且页面显示不起作用 行数限定无作用 换成下面的写法 overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 1;-webkit-box-orient: vertical;

CSS超出部分省略号

1&#xff0c;单行文本实现超出部分省略号 overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用省略号显示 white-space:nowrap; //溢出不换行 2&#xff0c;多行文本实现超出部分省略号 overflow:hidden; text-overflow:ellipsis; display:-webkit-b…

css文字超出部分省略号代替

问题 之前做过好过网页&#xff0c;其中经常遇到一个常用的知识点&#xff0c;就是当文字超出界面的范围&#xff0c;用省略号代替显示&#xff0c;给网页一美观。 解决方案 1.单行显示省略号&#xff1a; 点击查看代码详情 效果如下: 2.多行显示省略号&#xff1a; 点击…

CSS文字超出部分省略号显示

文章目录[隐藏] CSS 文字超出部分省略号显示前言CSS 文字超出部分省略号显示实现方法CSS 文字超出部分省略号显示实现效果 CSS 文字超出部分省略号显示前言 在我们开发过程中&#xff0c;其实有这样的需求&#xff1a; 在一个文章列表中&#xff0c;文章摘录太长了会影响排版格…

css 文字换行和超出部分省略号

css 文字换行和超出部分省略号 文字空格与换行 当文本溢出父级 block 元素时, 文本会换行; 这个特性是通过 white-space 控制的; 它影响浏览器处理空格, 换行和 tab 的处理; 它有下面几种值 normal, 默认值, 连续的空白符会被合并&#xff0c;换行符会被当作空白符来处理。…