Gau*_*tam 39 xml android android-ui android-theme
Android风格和主题似乎总是让我头晕目眩.我想在我的应用程序中使用不同版本的Android的Holo UI.所以我决定通过浏览源来提取必要的资源.
我遇到了以下情况android-15\data\res\values\themes.xml,我很困惑究竟是什么'继承',从哪里来:
<style name="Theme.Holo.Light" parent="Theme.Light">
...
...
</style>
Run Code Online (Sandbox Code Playgroud)
如果要继承自己定义的样式,则不必使用该
parent属性.相反,只需将要继承的样式的名称添加到新样式的名称前面,并用句点分隔.
但是从上面的代码来看,它似乎Theme.Holo.Light是继承Theme.Holo而来的Theme.Light.
这是如何工作的,或者我没有正确阅读的内容?
Shi*_*esh 68
我一直在想这个,所以我写了一个简单的测试应用程序来试试.资源文件如下所示:
<!--
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>
Run Code Online (Sandbox Code Playgroud)
所以我将AppTheme.TestTheme应用于清单文件中的活动.AppTheme使窗口全屏,没有标题栏.Theme.Light使窗口背景亮而不是默认的暗.当parent="android:Theme.Light"指定属性,窗口为白色,而不是全屏-这意味着parent="..."属性优先名称的前缀,而层次似乎是TestTheme <- Theme.Light (light) <- Theme (dark).
删除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的轻型版本".而且因为他们想要让人感到困惑.
这是在Gingerbread 2.3.3上测试的.