一、在themes.xml中添加自定義dialog的樣式
<!--自定義Dialog-->
<style name="BaseDialog" parent="android:style/Theme.Dialog">
<!--背景顏色和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除標(biāo)題-->
<item name="android:windowNoTitle">true</item>
<!--是否去除邊框-->
<item name="android:windowFrame">@null</item>
<!--是否浮現(xiàn)在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
二鉴象、創(chuàng)建dialog基類(lèi)
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基類(lèi)
*/
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() {
// 避免用戶(hù)快速雙擊重復(fù)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
}
}
}
三忙菠、創(chuàng)建自定義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="是否全部標(biāo)記為已讀?"
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>
四纺弊、創(chuàng)建自定義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()