Android XML主题继承自两个父主题?

时间:2012-09-27 05:34:53

标签: android xml android-ui android-theme

Android风格和主题似乎总是让我头晕目眩。我想在我的应用程序中使用不同版本的Android的Holo UI。所以我决定通过浏览源来提取必要的资源。

我在android-15\data\res\values\themes.xml中遇到了以下内容,我对于'继承'究竟是什么以及从哪里感到困惑:

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

Android API Guide说:

  

如果你想继承你自己定义的样式,那么你   不必使用parent属性。相反,只需在名称前加上前缀   您要继承的样式的样式,   被一段时间隔开。

但是从上面的代码中,似乎Theme.Holo.Light继承自Theme.HoloTheme.Light

这是如何工作的,或者我没有正确阅读的内容?

1 个答案:

答案 0 :(得分:66)

我一直在想这个,所以我写了一个简单的测试应用来试试。资源文件如下所示:

<!--
    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使窗口背景亮而不是默认的暗。指定parent="android:Theme.Light"属性时,窗口为白色而不是全屏 - 这意味着parent="..."属性优先于名称前缀,层次结构显示为TestTheme <- Theme.Light (light) <- Theme (dark)。< / p>

删除parent =“android:Theme.Light”后,屏幕显示为黑屏和全屏,因此TestTheme <- AppTheme (fullscreen) <- AppBaseTheme <- Theme (dark)层次结构就位。

指定parent="..."时,删除前缀时没有区别。所以parent="..."似乎绝对优先。 AppTheme.TestTheme不会立即从父母那里继承。

现在,查看默认的themes.xml,似乎Theme.Holo.Light继承自Theme.Light,然后在其描述中手动指定所有Holo内容。所以他们将它命名为Theme.Holo.Light并不是因为它继承了Holo,而是因为他们想要一个名称将其描述为“Holo的轻型版本”。而且因为他们希望成为$ @&amp;令人困惑的。

这是在Gingerbread 2.3.3上测试的。