【问题标题】:How to apply themes for an exact view?如何应用主题以获得精确视图?
【发布时间】:2012-11-15 21:17:24
【问题描述】:
例如,当我在 XML 中定义样式标签时,所有类型的所有视图都会获得该主题。视图类型的样式之间的差异有什么解决方案吗?这是一些代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonTheme" parent="android:Theme.Holo">
<item name="android:drawableLeft">@drawable/ic_launcher</item>
</style>
<style name="textTheme" parent="android:Theme.Holo">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
【问题讨论】:
标签:
android
view
styles
themes
【解决方案1】:
我认为风格和主题之间存在一些小的混淆。您正在谈论为应用程序中的小部件定义自定义样式。主题是全局应用于活动或应用程序的这些样式的集合。您为按钮和文本视图创建的自定义样式可以应用于新的自定义主题,以便所有按钮和文本项共享相同的属性。
我认为您正在寻找的东西更像这样。
<style name="ApplicationTheme" parent="android:Theme.Holo">
<item name="buttonStyle">@style/MyButtonStyle</item>
<item name="textAppearance">@style/MyTextAppearance</item>
</style>
<style name="MyButtonStyle" parent="android:Widget.Button">
<item name="android:drawableLeft">@drawable/ic_launcher</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#ff0000</item>
</style>
在这里,我们创建了两种新样式(一种用于按钮外观,另一种用于默认文本外观),然后将它们作为属性应用到新的自定义主题中。您现在可以通过引用 @style/ApplicationTheme 或 R.style.ApplicationTheme 在清单或视图构造函数中应用此主题
【解决方案2】:
您可以通过将属性 android:theme="" 添加到您的 AndroidManifest.xml 文件中的特定活动来做到这一点
例子:
<activity android:theme="@style/CustomTheme">