Android Activity 的对话框(Dialog)样式
Android Activity 的对话框(Dialog)样式
Activity
可以设置成 dialog 样式出现,主要通过 manifest 配置中 <activity>
标签的 android:theme
属性值实现,亦或通过代码设置方式 setTheme(int)
修改 Activity
出现的样式。
这篇文章中测试设备是一台手机。
程序基本构建配置:
android {compileSdk = 35defaultConfig {applicationId = "com.sanren1024.dialog"minSdk = 30this.targetSdk = 35versionCode = 1versionName = "1.0.0.0"testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"}
// ...
}
下面列举若干个 theme 对应的页面出现的样式,通过代码方式设置 theme.
android.R.style.Theme_Dialog
这个普通的 Dialog 样式,ActionBar 背景色是黑色。
Material
android.R.style.Theme_Material_Dialog
这个样式中,整个背景是黑色,带有 ActionBart 的一个 Dialog 样式。与上面的区别是在 ActionBar 的颜色不同,布局有所不同。
android.R.style.Theme_Material_Dialog_Alert
这个 Dialog 样式与 android.R.style.Theme_Material_Dialog
一致。
android.R.style.Theme_Material_DialogWhenLarge
这个样式的 Activity,基本与不设置 Dialog 样式的 Activity 一致,不类似于上面几个样式,会修改底部 NavigationBar 的背景色,这一个样式没有修改它的颜色。
android.R.style.Theme_Material_DialogWhenLarge_NoActionBar
这个样式在 android.R.style.Theme_Material_DialogWhenLarge
基础上移除了 ActionBar。
android.R.style.Theme_Material_Dialog_MinWidth
android.R.style.Theme_Material_Dialog_NoActionBar
android.R.style.Theme_Material_Dialog_NoActionBar_MinWidth
android.R.style.Theme_Material_Dialog_Presentation
这个样式比较不同,将 StatusBar 都遮蔽了。
Material_Light
android.R.style.Theme_Material_Light_Dialog
android.R.style.Theme_Material_Light_Dialog_Alert
android.R.style.Theme_Material_Light_Dialog_Presentation
android.R.style.Theme_Material_Light_DialogWhenLarge
android.R.style.Theme_Material_Light_Dialog_MinWidth
android.R.style.Theme_Material_Light_DialogWhenLarge_DarkActionBar
android.R.style.Theme_Material_Light_DialogWhenLarge_NoActionBar
android.R.style.Theme_Material_Light_Dialog_NoActionBar
android.R.style.Theme_Material_Light_Dialog_NoActionBar_MinWidth
DeviceDefault
android.R.style.Theme_DeviceDefault_Dialog
android.R.style.Theme_DeviceDefault_DialogWhenLarge
android.R.style.Theme_DeviceDefault_Dialog_MinWidth
android.R.style.Theme_DeviceDefault_DialogWhenLarge_NoActionBar
android.R.style.Theme_DeviceDefault_Dialog_Alert
android.R.style.Theme_DeviceDefault_Dialog_NoActionBar
android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth
DeviceDefault_Light
android.R.style.Theme_DeviceDefault_Light_Dialog
android.R.style.Theme_DeviceDefault_Light_DialogWhenLarge
android.R.style.Theme_DeviceDefault_Light_DialogWhenLarge_NoActionBar
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar
android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth
android.R.style.Theme_DeviceDefault_Light_Dialog_MinWidth
Overlay
android.R.style.ThemeOverlay_Material_Dialog
android.R.style.ThemeOverlay_Material_Dialog_Alert
总结
Dialog 样式主要的风格是 Material 样式和 DeviceDefault 样式。
两者的主要区别如下表。
Material | DeviceDefault |
---|---|
遵循 Google 的 Material Design 设计预言,强调阴影、圆角、动态色彩和交互动画(如水波纹效果)。 | 继承设备制造商(如三星 One UI、小米 MIUI)的自定义主题,视觉风格由厂商定义。 |
仅支持 Android 5.0+(API 21) | 从 Android 4.0(API 14)开始 |
自定义灵活性高 | 自定义灵活性低 |
视觉效果一致 | 因厂商而异 |