使用属性androidTextapperance与属性样式时的继承问题 [英] Inheritance issue, when using property androidTextapperance vs. property style

查看:112
本文介绍了使用属性androidTextapperance与属性样式时的继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根主题和一个继承子主题:

I have a root theme and an inheriting child theme:

<resources>
    <color name="colorRed">#FF0000</color>
    <color name="colorGreen">#00FF00</color>

    <style name="Root" parent="Theme.AppCompat.Light.DarkActionBar" />

    <style name="Root.TextAppearance" parent="android:TextAppearance">
        <item name="android:textColor">@color/colorGreen</item>
    </style>

    <style name="Child" parent="Root">
        <item name="android:textColor">@color/colorRed</item>
    </style>

</resources> 

在清单文件中,设置主题Child.在布局中,我应用了根主题的文本外观:

In the manifest file I set the theme Child. In the layout I apply the text appearance from the root theme:

<TextView
    android:textAppearance="@style/Root.TextAppearance"
    android:text="Hello World!"

  • 预期:绿色文本
  • 实际:红色文字
  • Root.TextAppearance如何继承红色?

    推荐答案

    检查优先级顺序

    该问题的简短答案是,主题优先于android:textAppearance.

    Android有不同类型的层次结构.使用styles属性时,样式层次结构将按预期应用.假定styles层次结构也适用于android:textAppearance属性,显然会失败.

    There are different types of hierarchies for Android. When using the styles attribute the styles hierarchy applies as expected. Assuming that the styles hierarchy also applies for the android:textAppearance attribute obviously fails.

    主题还有另一种层次结构.该层次结构遵循布局树.主题Child被应用在清单中,并且是顶层.

    There is another hierarchy for themes. This hierarchy follows down the layout tree. The theme Child is applied in the manifest and is the top level.

    似乎此顶级主题引入的设置甚至取代了较低级别上的android:textAppearance设置.仍然感觉不对,因为较低的级别通常应该覆盖较高的级别.

    It seems the settings coming in by this top level theme even overrule the settings of android:textAppearance on a lower level. This still feels wrong, as lower levels usually should overwrite higher levels.

    因此,我通过应用属性styleandroid:themeandroid:textAppearannce找出它们的强度顺序来进行一些测试.

    So I do some tests by applying the attributes style, android:theme and android:textAppearannce to find out the order of their strengths.

    事实证明,texAppearance是最弱的,而style是最强的:

    It turns out that texAppearance is the weakest and style is the strongest:

    1. style
    2. android:theme
    3. android:textAppearance
    1. style
    2. android:theme
    3. android:textAppearance

    我认为这是为什么较高级别的theme可以覆盖较低级别的android:textAppearance的原因.我承认我发现textAppearance的低优先级相当令人困惑.这不是我期望的那样.但是,这就是我发现正在测试的结果.

    I think this the explanation why the theme from the higher level could overwrite the android:textAppearance on the lower level. I admit that I find the low precedence of textAppearance rather confusing. It's not how I expected it to work. However, that's the results I found out be testing.

    这篇关于使用属性androidTextapperance与属性样式时的继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆