Android的XML主题从两个父主题继承?主题、两个、Android、XML

2023-09-06 13:24:12 作者:丶半冷清歌

Android的风格和主题似乎总是使我目瞪口呆。我想用全息UI在不同的Andr​​oid版本为我的应用程序。所以我决定通过浏览源中提取必要的资源。

Android styles and themes always seem to make my head spin. I wanted to use the Holo UI across different versions of Android for my app. So I decided to extract the necessary resources by browsing the source.

我在的android-15 \数据遇到以下\水库\值\的themes.xml ,我很困惑,究竟是被继承和从那里:

I came across the following in android-15\data\res\values\themes.xml and I'm confused as to what exactly is being 'inherited' and from where:

<style name="Theme.Holo.Light" parent="Theme.Light">
    ...
    ...
</style>

借助 Android的API指南说:

如果你想从你自己定义的样式继承时,   不必使用属性。相反,只要preFIX名称   的风格要继承给你的新样式的名称,   用句点分隔。

If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period.

不过从code以上,这似乎是 Theme.Holo.Light Theme.Holo 和 Theme.Light

But from the code above, it seems like Theme.Holo.Light is inheriting from Theme.Holo and from Theme.Light.

是如何工作的,或者是我是不是看是否正确?

How does that work, or what am I not reading properly?

推荐答案

我一直在想这个一样,所以我写了一个简单的测试应用程序来试试吧。资源文件是这样的:

I've been wondering about this as well, so I wrote a simple test app to try it. Resources file looks like this:

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

<style name="AppTheme.TestTheme" parent="android:Theme.Light">

</style>

所以我申请AppTheme.TestTheme到活动中的清单文件。 AppTheme使窗口全屏和放大器;没有标题栏。 Theme.Light使窗口背景光,而不是默认的黑暗。当父=机器人:Theme.Light指定的属性,则窗口是白色的,而不是全屏 - 这意味着父= ...属性接受precedence在名称preFIX和层次显得 TestTheme&LT; - Theme.Light(光)&LT; - 主题(深)

So I apply AppTheme.TestTheme to the activity in the manifest file. AppTheme makes the window full-screen & not have a title bar. Theme.Light makes the window background light instead of the default dark. When the parent="android:Theme.Light" attribute is specified, the window is white and not fullscreen -- this means that the parent="..." attribute takes precedence over the name prefix, and the hierarchy appears to be TestTheme <- Theme.Light (light) <- Theme (dark).

通过父=机器人:Theme.Light去掉,画面是黑暗和全屏,所以 TestTheme&LT; - AppTheme(全屏)&LT; - AppBaseTheme&LT; - 主题(暗)层次到位。

With parent="android:Theme.Light" removed, the screen is dark and fullscreen, so the TestTheme <- AppTheme (fullscreen) <- AppBaseTheme <- Theme (dark) hierarchy is in place.

父=...被指定,这都没有区别,当我删除preFIX与否。因此,父=...似乎采取绝对precedence。 AppTheme.TestTheme没有从父母双方继承了一次。

When parent="..." is specified, it makes no difference when I remove the prefix or not. So parent="..." seems to take definitely precedence. AppTheme.TestTheme does not inherit from both parents at once.

现在,看着默认的themes.xml,似乎Theme.Holo.Light从Theme.Light继承,然后把所有的全息材料中指定手动修改其描述。因此,他们把它命名为Theme.Holo.Light不是因为它从全息继承,而是因为他们希望这将它描述为光全息版的名称。而且因为他们想成为$ @&安培;!。荷兰国际集团迷惑

Now, looking at the default themes.xml, it seems that Theme.Holo.Light inherits from Theme.Light, and then all of the Holo stuff is specified manually in it's description. So they named it Theme.Holo.Light not because it inherits from Holo, but because they wanted a name that describes it as 'the light version of Holo'. And because they wanted to be $@&!ing confusing.

这是在姜饼2.3.3测试。

This was tested on Gingerbread 2.3.3.