一、在themes.xml中添加自定义dialog的样式
<!--自定义Dialog-->
<style name="BaseDialog" parent="android:style/Theme.Dialog">
<!--背景颜色和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题-->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
二、创建dialog基类
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import com.example.myapplication.R
/**
* dialog基类
*/
abstract class BaseDialog(context: Context) : Dialog(context, R.style.BaseDialog) {
companion object {
var currentTime = 0L
}
abstract fun layoutResID(): Int
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layoutResID())
}
override fun show() {
// 避免用户快速双击重复show两次
if (System.currentTimeMillis() - currentTime > 500) {
currentTime = System.currentTimeMillis()
super.show()
val layoutParams = window?.attributes
layoutParams?.gravity = Gravity.CENTER
layoutParams?.width = ViewGroup.LayoutParams.MATCH_PARENT
layoutParams?.height = ViewGroup.LayoutParams.MATCH_PARENT
window?.decorView?.setPadding(0, 0, 0, 0)
window?.attributes = layoutParams
}
}
}
三、创建自定义dialog的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#60000000"
android:orientation="vertical">
<LinearLayout
android:layout_gravity="center"
android:background="@drawable/shape_dialog"
android:layout_width="270dp"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_marginTop="25dp"
android:layout_gravity="center_horizontal"
android:text="是否全部标记为已读?"
android:textColor="@color/black"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_marginTop="26dp"
android:background="#E6E6E6"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_cancel"
android:textSize="16sp"
android:textColor="@color/black"
android:gravity="center"
android:text="取消"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<View
android:background="#E6E6E6"
android:layout_width="1dp"
android:layout_height="match_parent"/>
<TextView
android:textSize="16sp"
android:textColor="@color/black"
android:gravity="center"
android:text="确定"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
四、创建自定义dialog
import android.content.Context
import android.os.Bundle
import com.example.myapplication.base.BaseDialog
import kotlinx.android.synthetic.main.dialog_main.*
class MainDialog(context: Context) : BaseDialog(context) {
override fun layoutResID(): Int = R.layout.dialog_main
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
tv_cancel.setOnClickListener { dismiss() }
}
}
五、在activity中使用自定义dialog
val dialog = MainDialog(context)
dialog.show()