使用AppCompact时是否需要特定于版本的样式项?

时间:2016-12-15 14:47:00

标签: android styles android-appcompat

请检查选项1和选项2,在values-v21样式中定义平台特定样式属性(如android:colorPrimary)的优势是什么。

值/ styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="CommonTheme">

    </style>

    <style name="CommonTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

选项1:values-v21 / styles.xml - 通过继承将AppCompact用于所有版本。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="CommonTheme">
        <!-- All customization of the theme for this version -->
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

选项2:values-v21 / styles.xml - 编写特定于平台的样式属性以及继承。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="CommonTheme">
        <item name="android:colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:colorAccent">@color/colorAccent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

我在不同的地方看到两种选择,哪种更好?我们是否在选项2中获得任何优势。?

1 个答案:

答案 0 :(得分:2)

选项2完全没必要,因为AppCompat本身会复制颜色,如values-v21 theme所示:

<!-- Copy our color theme attributes to the framework -->
<item name="android:colorPrimary">?attr/colorPrimary</item>
<item name="android:colorPrimaryDark">?attr/colorPrimaryDark</item>
<item name="android:colorAccent">?attr/colorAccent</item>
<item name="android:colorControlNormal">?attr/colorControlNormal</item>
<item name="android:colorControlActivated">?attr/colorControlActivated</item>
<item name="android:colorControlHighlight">?attr/colorControlHighlight</item>
<item name="android:colorButtonNormal">?attr/colorButtonNormal</item>