java 更改导航栏颜色,Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27589833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:04:09  来源:igfitidea点击:

Change navigation bar color, Android

javaandroidxml

提问by Marco Gagino

How can I change the color/transparency of the Navigation Bar from black to a generic color in pre-Lollipop devices (e.g. the color of the status bar or of the action bar)?

如何在 Lollipop 之前的设备中将导航栏的颜色/透明度从黑色更改为通用颜色(例如状态栏或操作栏的颜色)?

Can I do it using AppCompat or is it only possible for SDK 21?

我可以使用 AppCompat 来完成还是仅适用于 SDK 21?

回答by Floern

You can set the attribute navigationBarColorin an AppCompat theme, or android:navigationBarColorin a native v21 theme.

您可以navigationBarColor在 AppCompat 主题或android:navigationBarColor原生 v21 主题中设置该属性。

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    ...
    <item name="navigationBarColor">#123456</item>
</style>

https://developer.android.com/training/material/theme.html#StatusBar

https://developer.android.com/training/material/theme.html#StatusBar

Note that this does not work on Pre-Lollipop devices, since this feature needs to be supported by the system, which is not the case on Android 4.4 or older.

请注意,这不适用于 Pre-Lollipop 设备,因为此功能需要系统支持,而在 Android 4.4 或更早版本上则不是这种情况。

回答by Terranology

Another programmatically way:

另一种编程方式:

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.DarkOrange));
window.setNavigationBarColor(getResources().getColor(R.color.red));

Further, to change your status bar color, add the following line:

此外,要更改状态栏颜色,请添加以下行:

window.setStatusBarColor(getResources().getColor(R.color.green));

回答by hedgehog

style-v21

风格-v21

<resources>

<style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowTranslucentNavigation">true</item>//translucent
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@android:color/transparent</item>
</style>