【问题标题】:Android - Theme Alert DialogsAndroid - 主题警报对话框
【发布时间】:2017-08-14 00:25:46
【问题描述】:

我目前正在尝试在我的应用程序中实现主题,但在尝试设置对话框样式时遇到了问题。

<style name="MyTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="android:colorBackground">@color/background</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    ...
    <!-- AppCompat Dialog Attributes -->
    <item name="dialogTheme">@style/AppCompatDialog</item>
    <item name="alertDialogTheme">@style/AppCompatAlert</item>
</style>

例如,MyTheme 正确设置了 colorPrimary 和 colorBackground,这在应用程序的大多数区域都可以正常工作。当我需要引用它们时,我会像这样使用它们。

<LinearLayout
android:background="?android:colorBackground"
...>

    <TextView
    android:id="@+id/title"
    style="@style/TextAppearance.AppCompat.Display1"
    android:background="?attr/colorPrimary"
    ... />

但是,当我设置 dialogTheme 和 alertDialogTheme 的样式时,颜色似乎默认为其他颜色。

<style name="MyAlertDialogStyle" parent="Base.Theme.AppCompat.Dialog">
    ...
    <item name="android:background">?android:colorBackground</item>
</style>

这不会使用我定义的颜色背景,而是一些浅灰色(默认?)颜色。

这是一个结果示例。

<style name="MyAlertDialogStyle" parent="Base.Theme.AppCompat.Dialog">
    <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
    <!-- Used for the buttons -->
    <item name="android:colorAccent">?colorAccent</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">?colorAccent</item>
    <!-- Used for the background -->
    <item name="android:background">?android:colorBackground</item>
</style>

左侧(“Desired”)使用的是普通颜色,例如 @color/colorAccent,而右侧(“Result”)是我尝试使用主题颜色 ?colorAccent 时会发生的情况。

如何在样式中正确引用我的主题颜色?


另外,有些相关的问题,我正在尝试设置我的工具栏的样式,但它没有任何效果。

<style name="MyTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar" >
    ...
    <item name="toolbarStyle">@style/toolbarStyle</item>
</style>

<style name="toolbarStyle" parent="Base.Widget.AppCompat.Toolbar">
    <item name="android:background">@drawable/header_background</item>
</style>

知道为什么这不起作用吗?

谢谢!

【问题讨论】:

  • 尽可能添加屏幕截图,以便我们了解错误。
  • 我添加了一张图片来展示我使用主题方法引用颜色所得到的结果。这有帮助吗?
  • 感谢您提供更多信息。如果要执行此类操作,请使用 DialogFragment。它们与对话框相同,但您可以对其进行更多控制。
  • 你能分享你创建和显示对话框的代码吗?
  • 我所做的只是new AlertDialog.Builder(context, R.style.MyAlertDialogStyle);,传递我上面定义的样式。也许我应该改用ContextThemeWrapper

标签: android android-theme android-styles


【解决方案1】:

通过在 Activity 的主题中设置此属性,我至少能够为 API >= 23 中的对话框背景设置主题:

<style name="AppTheme" parent="Theme.AppCompat">
  <item name="android:colorBackgroundFloating">@color/gunmetal</item>
</style>

然后确保您的活动在您的 AndroidManifest.xml 中使用它:

<activity
        android:name="package.your.YourActivity"
        android:theme="@style/AppTheme"
...

【讨论】:

    【解决方案2】:

    在扩展 DialogFragment 时,请改用 AppCompatDialogFragment 并在您的主题中

    <style name="YourAppTheme" parent="Theme.AppCompat">
        <item name="colorBackgroundFloating">your_desired_color</item>
    </style>
    

    没有android 前缀。

    经过 API 21+ 测试

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2017-05-10
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多