Android XML theme inheriting from two parent themes?

后端 未结 1 1307
太阳男子
太阳男子 2020-12-13 09:11

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 necess

相关标签:
1条回答
  • 2020-12-13 09:33

    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>
    

    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).

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

    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.

    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 its 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.

    This was tested on Gingerbread 2.3.3.

    0 讨论(0)
提交回复
热议问题