10

我有以下主题res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppBaseTheme" parent="@android:style/Theme.NoTitleBar">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

如果我删除该<item name="android:windowNoTitle">true</item>行,我的活动就会有标题栏。但是,如果我将那条线保留在那里,那么它们就没有标题栏(这是我想要的)。

我很好奇为什么parent="@android:style/Theme.NoTitleBar"似乎没有效果?parent不做我认为的事情?我认为它是这样工作的:

@android:style/Theme.NoTitleBar --> AppBaseTheme --> AppTheme
^                                   ^                ^
|                                   |                |
|                                   |                Has everything AppBaseTheme
|                                   |                does, unless it's overridden
|                                   |
|                                   Has everything @android:style/Theme.NoTitleBar
|                                   does, unless it's overridden (which I'm not
|                                   doing)
|
Sets whatever it needs to to not have a title, which I assume
is done by setting <item name="android:windowNoTitle">true</item>

但是,我也发现如果我设置<item name="android:windowNoTitle">true</item>AppBaseTheme也没有效果;我必须将它设置在AppTheme. 那么指定父级到底有什么意义呢?

4

1 回答 1

9

我想我刚刚想通了。事实证明,当我创建项目时,Android 创建了一个res/values-14/styles.xml文件,里面是<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">.

It turns out that the res/values-14/styles.xml file was overriding my default res/values/styles.xml because the device I was testing on has API level 14, and thus it preferred the files in values-14 than the default values.

Inheritance does seem to work like I thought it did; I just wasn't realizing things were being overridden by the files created when I first made my project.

于 2013-01-25T22:14:26.930 回答