在新的棒棒糖(update)更新中,我注意到使用本地的Google应用程序时,状态栏的颜色会改变以匹配您运行的应用程序的操作栏(action bar)。我还看到推特(Twitter)应用程序上也有这个功能,所以我猜不仅限于Google可以做到这一点。
如果可能,有人知道如何做到这一点吗?
如果可能,有人知道如何做到这一点吗?
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(activity, R.color.example_color));
请记住根据Material Design指南状态栏颜色和操作栏颜色应该不同:
请查看下面的截图:
只需将以下代码添加到styles.xml文件中。 colorPrimary用于操作栏,而colorPrimaryDark用于状态栏。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
</style>
这张来自Android开发者的图片更详细地解释了色彩板。您可以在此链接中阅读更多内容。
<color name="colorPrimary">#somecolor</color>
和 <color name="colorPrimaryDark">#somecolor</color>
。它们可以被更改以达到所需的效果。 - Neurotransmitter通过style.xml还可设置状态栏的颜色。
为此,请在res/values-v21文件夹下创建一个style.xml文件,并输入以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material">
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">@color/blue_dark</item>
</style>
</resources>
编辑: 如评论所指出的,使用AppCompat时代码是不同的。在文件res/values/style.xml中,请使用以下代码:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- Set AppCompat’s color theming attrs -->
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
<!-- Other attributes -->
</style>
要设置状态栏颜色,请在res/values-v21文件夹下创建一个style.xml文件,并使用以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/blue</item>
</style>
</resources>
如果您想为不同的活动(fragment)设置不同的状态栏颜色,您可以按照以下步骤进行操作(适用于API 21及以上):
首先创建values21/style.xml
并添加以下代码:
<style name="AIO" parent="AIOBase">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowContentTransitions">true</item>
</style>
values/style.xml
中定义白色|暗色主题,如下所示: <style name="AIOBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/color_primary_dark
</item>
<item name="android:textColor">@color/gray_darkest</item>
<item name="android:windowBackground">@color/default_bg</item>
<item name="android:colorBackground">@color/default_bg</item>
</style>
<style name="AIO" parent="AIOBase" />
<style name="AIO.Dark" parent="AIOBase">
<item name="android:statusBarColor" tools:targetApi="lollipop">#171717
</item>
</style>
<style name="AIO.White" parent="AIOBase">
<item name="android:statusBarColor" tools:targetApi="lollipop">#bdbdbd
</item>
</style>
manifest.xml
中应用主题。 <item name="android:statusBarColor">#43434f</item>
在 Android Lollipop 之前的设备中,您可以通过 SystemBarTintManager 来实现此功能。 如果您正在使用 Android Studio,请在 gradle 文件中添加 Systembartint 库。
dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
...
}
然后在你的活动中
// create manager instance after the content view is set
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
// enable status bar tint
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setTintColor(getResources().getColor(R.color.blue));
getWindow().setStatusBarColor(activity.getResources().getColor(R.color.example_color));
,它完美地工作了。不确定在哪些情况下标志是严格必要的。 - Joaquin Iurchuk