这是为不同版本提供不同值/样式的正确方法

ant*_*009 22 android

Android Studio 2.1 preview 3
Run Code Online (Sandbox Code Playgroud)

这只是一个问题,因为我很困惑,因为我已经看到了许多替代方案.

我创建了一个新的android项目,我的Activity扩展了AppCompatActivity

public class MainActivity extends AppCompatActivity {
Run Code Online (Sandbox Code Playgroud)

我希望transparent statusbar21台及以上的设备运行.

所以在我,values/styles我有以下

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

在我,values-21/styles我有以下

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- Make the statusbar transparent -->
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

我的清单我选择了主题

android:theme="@style/AppTheme"
Run Code Online (Sandbox Code Playgroud)

只是一些问题

  1. 这是正确的方法,还是有更好的方法吗?
  2. values-21/styles继承所有的颜色,values/styles所以我不得不重复这个吗?

Mim*_*oli 40

这是正确的方式.我可以建议你更好地组织你的风格吗?

价值观/ 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>
Run Code Online (Sandbox Code Playgroud)

值-V21/styles.xml

<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>
Run Code Online (Sandbox Code Playgroud)

因此,您无需为每个api级别重复该样式的常用值.


ran*_*dom 12

我将尝试回答它给出一些参考

保持兼容性

为了避免重复代码,在res/values /中定义样式,修改res/values-v21 /中新样式的样式,并使用样式继承,在res/values /中定义基本样式,并继承res/values中的样式值-V21 /

所以,你应该尽量避免重复代码在style.xml不同的文件夹res/values/,并res/values-v21/通过使用样式继承.

风格继承

如果要继承自己定义的样式,则不必使用parent属性.相反,只需将要继承的样式的名称添加到新样式的名称前面,并用句点分隔.

如果要继承自己定义的样式,可以跳过添加parent属性并使用点或句点表示法从中继承.

使用此功能,您可以使用不同的颜色定义基本主题BaseTheme,res/values/并从中继承,BaseTheme.StyledStatusBar而不指定父属性.

<resources>
    <style name="BaseTheme.StyledStatusBar"></style>

    <!-- Base application theme. -->
    <style name="BaseTheme" 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>
Run Code Online (Sandbox Code Playgroud)

values-21/中,添加项目android:windowTranslucentStatusBaseTheme.StyledStatusBar

<resources>
    <style name="BaseTheme.StyledStatusBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

在清单中,选择主题

android:theme="@style/BaseTheme.StyledStatusBar"
Run Code Online (Sandbox Code Playgroud)


Bud*_*ius 5

1)这是正确的方法,还是有更好的方法?

是的。这是针对不同 API 版本使用不同值的正确/推荐方法。

2)values21/styles会继承values/styles中的所有颜色,所以我必须重复这个吗?

我不确定我是否完全理解这个问题。您显示的两种样式都将继承Theme.AppCompat.Light.NoActionBar,因此您的颜色应该再次声明,但我将提供两种更好的选择:

替代方案 1,更好一点:

使用两者通用的基本主题。要查看它的代码,请检查@mimmo-grottoli 答案。

替代方案 2,更好:

如果两个主题唯一的不同是android:windowTranslucentStatusKitKat(API 级别 19)中引入的主题,您可以将它们全部放在值/样式中的同一主题中,如下所示:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- Make the statusbar transparent -->
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

Android 框架会忽略它无法识别的 XML 参数。这意味着在 JellyBean 或 ICS 上,设备windowTranslucentStatus在正确应用颜色时将忽略,而在 KitKat 及更高版本中,它将正确应用windowTranslucentStatus

这个技巧对于 Android 中的所有 XML(甚至布局)都有效,IDE 可能会向您提供有关 API 级别的警告,但在 XML 中它们始终可以安全使用。