[Android] Comprehensive list of commonly used dialog boxes (3) Three ways to customize Dialog (Theme, PopupWindow, Dialog)

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

[Android] Comprehensive list of commonly used dialog boxes (3) Three ways to customize Dialog (Theme, PopupWindow, Dialog)

Table of contents

Theme

Customize using Theme Dialog. Few people should know this method. If you want your dialog Dialogbox to have a life cycle, use this method to define a Dialogdialog box that has a life cycle.

Steps for usage:

1. Create aActivity

Insert image description here
2. Write layout files and styles

3. ActiviitySet 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:
Insert image description here
Created using this method Dialog, it appears to be an ordinary dialog box, but is actually an Activityinterface.

PopupWindow

PopupWindowA popup window that can be used to display any view.

PopupWindowAnimation can be used to display when popping up or hiding.

PopupWindowIt is a floating container that appears on top of the current activity. In view of this, there is Dialoganother way to customize it.

Steps for usage:

1. Create a layout file

2. Then PopupWindowinstantiate

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:

Insert image description here
PopupWindowIt 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.

PopupWindowFor other properties, please refer to: Android Developers - PopupWindow

Dialog

In Android, there are many dialog boxes based on Dialogimplementation, such as AlertDialog, MaterialAlertDialogBuilderetc.

Steps for usage:

1. Create a layout file

2. DialogInstantiate 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:
Insert image description here
Android can AlertDialoggenerally MaterialAlertDialogBuildermeet 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.


Click to download source code

References:
1. Detailed explanation of using PopUpWindow
2. Android Developers - Dialog
3. Android Developers - PopupWindow

Guess you like

Origin blog.csdn.net/baidu_41616022/article/details/125269616