【发布时间】: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