There are many ways to customize Android Dialog, such as: Theme, Dialog, PopupWindow, AlertDialog, View, etc.
Android common dialog box series of articles
[Android] Comprehensive list of commonly used dialog boxes (1) Android Dialog
【Android】Comprehensive list of commonly used dialog boxes (2) Material Dialog
Table of contents
Theme
Customize using Theme Dialog
. Few people should know this method. If you want your dialog Dialog
box to have a life cycle, use this method to define a Dialog
dialog box that has a life cycle.
Steps for usage:
1. Create aActivity
2. Write layout files and styles
3. Activiity
Set a theme for the created dialog box
<activity android:name=".ThemeDialogActivity"
android:theme="@style/Theme.Material3.Light.Dialog"></activity>
4. Start dialog box
startActivity(Intent(this, ThemeDialogActivity::class.java))
The code effect is as follows:
Created using this method Dialog
, it appears to be an ordinary dialog box, but is actually an Activity
interface.
PopupWindow
PopupWindow
A popup window that can be used to display any view.
PopupWindow
Animation can be used to display when popping up or hiding.
PopupWindow
It is a floating container that appears on top of the current activity. In view of this, there is Dialog
another way to customize it.
Steps for usage:
1. Create a layout file
2. Then PopupWindow
instantiate
val popupWindow = PopupWindow(this)
val inflate = LayoutInflater.from(this).inflate(R.layout.dialog_popupdialog, null, false)
popupWindow.contentView = inflate
// 解决布局无法覆盖屏幕边边的问题
popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
// 设置popupWindow宽高
popupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
popupWindow.height = ViewGroup.LayoutParams.MATCH_PARENT
// 设置点击外部是否隐藏
popupWindow.isOutsideTouchable = true
// 点击返回按钮隐藏
popupWindow.isFocusable = true
// 解决状态栏不被阴影布局覆盖问题
popupWindow.isClippingEnabled = false
// 设置显示的位置
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0)
The code effect is as follows:
PopupWindow
It should be the same as above Theme
. Few people use these two methods for customization Dialog
, because they are slightly more complicated than the third method, and the events of the components need to be rewritten by themselves, which will also It takes a lot of time.
PopupWindow
For other properties, please refer to: Android Developers - PopupWindow
Dialog
In Android, there are many dialog boxes based on Dialog
implementation, such as AlertDialog
, MaterialAlertDialogBuilder
etc.
Steps for usage:
1. Create a layout file
2. Dialog
Instantiate and display it
val dialog = android.app.Dialog(this)
val inflate = LayoutInflater.from(this).inflate(R.layout.dialog_viewpage, null, false)
// 设置Dialog的内容
dialog.setContentView(inflate)
dialog.show()
The code effect is as follows:
Android can AlertDialog
generally MaterialAlertDialogBuilder
meet everyone's daily needs. If you need to choose one of these three methods for customization, the simplest, most convenient and best-performing one should be customization Dialog
.
References:
1. Detailed explanation of using PopUpWindow
2. Android Developers - Dialog
3. Android Developers - PopupWindow