gpt4 book ai didi

android - Material Design 没有样式化警报对话框

转载 作者:IT老高 更新时间:2023-10-28 12:55:43 27 4
gpt4 key购买 nike

我已将 appCompat Material 设计添加到我的应用程序中,但警报对话框似乎没有使用我的主要颜色、主要颜色或强调色。

这是我的基本风格:

<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>

根据我的理解,对话框按钮文本也应该使用这些颜色。我的理解是错的还是我需要做更多的事情?


解决方案:

标记的答案让我走上了正轨。

<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>

最佳答案

2019 年 8 月更新,包含适用于 android 库的 Material 组件:

使用新的 Material components for Android library您可以使用新的 com.google.android.material.dialog.MaterialAlertDialogBuilder类,从现有的 androidx.appcompat.AlertDialog.Builder 扩展而来类并提供对最新 Material 设计规范的支持。

只要使用这样的东西:

new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();

您可以自定义扩展 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>

要应用您的自定义样式,只需使用构造函数:

new MaterialAlertDialogBuilder(context, R.style.CustomMaterialDialog)

enter image description here

自定义按钮、标题和正文 check this post了解更多详情。

您还可以全局更改应用主题中的样式:

 <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
<item name="materialAlertDialogTheme">@style/CustomMaterialDialog</item>
</style>

支持库和 APPCOMPAT 主题:

有了新的AppCompat v22.1,您可以使用新的android.support.v7.app.AlertDialog .

只需使用这样的代码:

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();

并使用这样的样式:

<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>

否则你可以在你当前的主题中定义:

<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- your style -->
<item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

然后在你的代码中:

 import android.support.v7.app.AlertDialog

AlertDialog.Builder builder =
new AlertDialog.Builder(this);

这里是 Kitkat 上的 AlertDialog: enter image description here

关于android - Material Design 没有样式化警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26455919/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com