3

我试图在我的所有应用程序中添加一个样式,所以我创建了一个包含我的样式的主题:

<resources>
    <style name="MyTheme" parent="android:Theme.Holo.Light">
        <item name="android:textAppearance">@style/subtitle</item>
    </style>

    <style name="subtitle parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/purple</item>
        <item name="android:textSize">40sp</item>
    </style>
</resources>

textAppearance不起作用它保持不变,但是当我textColor在我的主题中加入类似的东西时,它就起作用了

4

4 回答 4

8

这是一个很老的问题,但答案可能会对某人有所帮助。解决这个问题的关键是这里的“样式技术的优先顺序”:  不同样式技术图像的优先顺序

在顶部是最高优先级,在底部是最低优先级。

正如我们所看到theme的具有最低优先级,在您的示例中,您的android:textAppearance属性被default style接受此属性的每个视图覆盖,默认样式属性在每个接受此属性的特定视图中定义,在这种情况下android:Theme.Holo.Light提供textView 的默认样式为android:textViewStyle... 按钮是android:buttonStyle(从 TextView 继承的textAppearance),依此类推。

因此,如果您尝试将该android:textAppearance属性应用于 TextVew 您应该使用 <item name="android:textViewStyle">@style/subtitle</item>而不是<item name="android:textAppearance">@style/subtitle</item>inside MyTheme。远离将其设置android:textViewStyle为 null,这样您当前的代码将与textViews 一起正常工作<item name="android:textViewStyle">null</item>

这篇文章更深入地解释了这个优先级:

https://medium.com/androiddevelopers/whats-your-text-s-appearance-f3a1729192d

于 2019-05-25T02:29:53.450 回答
0

取决于您的目标 API,您需要将自定义代码放在不同的 /res/values-vxx/style.xml 文件中。

于 2013-08-11T20:17:14.600 回答
0

我可以看到,您没有在 xml 中为主题声明颜色。请在 中添加以下行<resources>并尝试。您的 xml 将如下所示:

<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:textAppearance">@style/subtitle</item>
</style>

 <color name="purple">code for your color</color>

<style name="subtitle parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/purple</item>
    <item name="android:textSize">40sp</item>
</style>

我认为这会做到。

于 2013-02-07T10:06:16.057 回答
0

对于 TextView,请尝试android:textAppearanceSmall在您的主题内。

于 2019-02-18T07:37:16.797 回答