Android Material Design 没有样式化警报对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26455919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Material Design not styling alert dialogs
提问by Matthew
I've added the appCompat material design to my app and it seems that the alert dialogs are not using my primary, primaryDark, or accent colors.
我已将 appCompat 材料设计添加到我的应用程序中,但似乎警报对话框没有使用我的主要颜色、primaryDark 或强调色。
Here is my base style:
这是我的基本风格:
<style name="MaterialNavyTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/apptheme_color</item>
<item name="colorPrimaryDark">@color/apptheme_color_dark</item>
<item name="colorAccent">@color/apptheme_color</item>
<item name="android:textColorPrimary">@color/action_bar_gray</item>
</style>
Based on my understanding the dialogs button text should also use these colors. Am I wrong on my understanding or is there something more I need to do?
根据我的理解,对话框按钮文本也应该使用这些颜色。我的理解有误还是我还需要做些什么?
Solution:
解决方案:
The marked answer got me on the right track.
明显的答案让我走上了正确的轨道。
<style name="MaterialNavyTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/apptheme_color</item>
<item name="colorPrimaryDark">@color/apptheme_color_dark</item>
<item name="colorAccent">@color/apptheme_color</item>
<item name="android:actionModeBackground">@color/apptheme_color_dark</item>
<item name="android:textColorPrimary">@color/action_bar_gray</item>
<item name="sdlDialogStyle">@style/DialogStyleLight</item>
<item name="android:seekBarStyle">@style/SeekBarNavyTheme</item>
</style>
<style name="StyledDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/apptheme_color</item>
<item name="colorPrimaryDark">@color/apptheme_color_dark</item>
<item name="colorAccent">@color/apptheme_color</item>
</style>
回答by Gabriele Mariotti
UPDATED ON Aug 2019 WITH The Material components for android library:
2019 年 8 月更新,包含适用于 android 库的 Material 组件:
With the new Material components for Android libraryyou can use the new com.google.android.material.dialog.MaterialAlertDialogBuilder
class, which extends from the existing androidx.appcompat.AlertDialog.Builder
class and provides support for the latest Material Design specifications.
借助适用于 Android 库的新Material 组件,您可以使用新com.google.android.material.dialog.MaterialAlertDialogBuilder
类,该类扩展了现有androidx.appcompat.AlertDialog.Builder
类并提供对最新 Material Design 规范的支持。
Just use something like this:
只需使用这样的东西:
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
You can customize the colors extending the ThemeOverlay.MaterialComponents.MaterialAlertDialog
style:
您可以自定义扩展ThemeOverlay.MaterialComponents.MaterialAlertDialog
样式的颜色:
<style name="CustomMaterialDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Background Color-->
<item name="android:background">#006db3</item>
<!-- Text Color for title and message -->
<item name="colorOnSurface">@color/secondaryColor</item>
<!-- Text Color for buttons -->
<item name="colorPrimary">@color/white</item>
....
</style>
To apply your custom stylejust use the constructor:
要应用您的自定义样式,只需使用构造函数:
new MaterialAlertDialogBuilder(context, R.style.CustomMaterialDialog)
To customize the buttons, the title and the body textcheck this postfor more details.
要自定义按钮、标题和正文,请查看此帖子以获取更多详细信息。
You can also change globallythe style in your app theme:
您还可以全局更改应用主题中的样式:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
<item name="materialAlertDialogTheme">@style/CustomMaterialDialog</item>
</style>
WITH SUPPORT LIBRARY and APPCOMPAT THEME:
支持库和APPCOMPAT主题:
With the new AppCompat v22.1
you can use the new android.support.v7.app.AlertDialog.
有了新的,AppCompat v22.1
您可以使用新的 android.support.v7.app.AlertDialog。
Just use a code like this:
只需使用这样的代码:
import android.support.v7.app.AlertDialog
AlertDialog.Builder builder =
new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ....");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
And use a style like this:
并使用这样的风格:
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFCC00</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#5fa3d0</item>
</style>
Otherwise you can define in your current theme:
否则,您可以在当前主题中定义:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- your style -->
<item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>
and then in your code:
然后在你的代码中:
import android.support.v7.app.AlertDialog
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
Here the AlertDialog on Kitkat:
这是 Kitkat 上的 AlertDialog:
回答by Sandeep Kumar
when initializing dialog builder, pass second parameter as the theme. It will automatically show material design with API level 21.
初始化对话框构建器时,将第二个参数作为主题传递。它将自动显示 API 级别 21 的材料设计。
AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
or,
或者,
AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
回答by nadavfima
AppCompat doesn't do that for dialogs (not yet at least)
AppCompat 不会为对话框执行此操作(至少目前还没有)
EDIT:it does now. make sure to use android.support.v7.app.AlertDialog
编辑:它现在。确保使用android.support.v7.app.AlertDialog
回答by Feng Dai
You can consider this project: https://github.com/fengdai/AlertDialogPro
可以考虑这个项目:https: //github.com/fengdai/AlertDialogPro
It can provide you material theme alert dialogs almost the same as lollipop's. Compatible with Android 2.1.
它可以为您提供与棒棒糖几乎相同的材料主题警报对话框。与安卓 2.1 兼容。
回答by Inoy
You could use
你可以用
Material Design Library
材料设计库
Material Design Librarymade for pretty alert dialogs, buttons, and other things like snack bars.Currently it's heavily developed.
Material Design Library 专为非常警觉的对话框、按钮和其他诸如小吃店之类的东西而设计。目前,它得到了大力发展。
Guide, code, example - https://github.com/navasmdc/MaterialDesignLibrary
指南、代码、示例 - https://github.com/navasmdc/MaterialDesignLibrary
Guide how to add library to Android Studio 1.0- How do I import material design library to Android Studio?
指导如何将库添加到Android Studio 1.0-如何将材料设计库导入到 Android Studio?
.
.
Happy coding ;)
快乐编码;)
回答by CaptRespect
For some reason the android:textColor only seems to update the title color. You can change the message text color by using a
出于某种原因, android:textColor 似乎只更新标题颜色。您可以使用
SpannableString.AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.MyDialogTheme));
AlertDialog dialog = builder.create();
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
dialog.setMessage(wordtoSpan);
dialog.show();
回答by David Vávra
Try this library:
试试这个库:
https://github.com/avast/android-styled-dialogs
https://github.com/avast/android-styled-dialogs
It's based on DialogFragments
instead of AlertDialogs
(like the one from @afollestad). The main advantage: Dialogs don't dismiss after rotation and callbacks still work.
它基于DialogFragments
而不是AlertDialogs
(就像来自@afollestad 的那个)。主要优点:旋转后对话框不会关闭,回调仍然有效。