我想将导航栏的颜色从黑色(带白色图标)更改为白色(带黑色图标)。
期望的结果(在uCrop中看到):
我知道如何通过android:windowLightStatusBar
更改状态栏的颜色。
导航栏是否有类似的属性?
我想将导航栏的颜色从黑色(带白色图标)更改为白色(带黑色图标)。
期望的结果(在uCrop中看到):
我知道如何通过android:windowLightStatusBar
更改状态栏的颜色。
导航栏是否有类似的属性?
对于 API 27+,您可以通过样式来实现:
<!-- added in API 27 -->
<item name="android:windowLightNavigationBar">true</item>
<!-- required to contrast the dark buttons -->
<item name="android:navigationBarColor">@android:color/white</item>
<!-- optional -->
<item name="android:navigationBarDividerColor">@android:color/black</item>
虽然XML属性是在API 27中引入的,但轻型导航栏的支持在更早的版本中就已经引入了,即通过API 26中的SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
。
因此,为了针对最早支持它的设备进行定位,您需要通过代码而不是XML样式来实现。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
...
<item name="android:navigationBarColor">@android:color/white</item>
</style>
</resources>
为了让导航栏中按钮的颜色与浅色背景相匹配,您需要在活动的DecorView上设置2个标志。
在您的活动中:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
从安卓 O 版本开始,这应该变得非常简单。 SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
android:windowLightNavigationBar
- MygodBaseTheme
中添加此内容。<item name="android:navigationBarColor">@color/yourNavigationColor</item>
我在所有的Activity中使用这个Kotlin扩展函数。只需将这些函数复制到您的项目中的某个位置,您可以随时使用它们来更改状态栏颜色或导航栏颜色,或更改浅色/深色状态栏或浅色/深色导航栏(如果可能)。
fun AppCompatActivity.setStatusBarColor(color: Int)
{
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.setStatusBarColor(color)
}
fun AppCompatActivity.setStatusLightDark(is_light: Boolean)
{
if (Build.VERSION.SDK_INT < 23)
{
return
}
var flags = window.decorView.systemUiVisibility
if (is_light)
{
flags = flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}
else
{
flags = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
window.decorView.systemUiVisibility = flags
}
fun AppCompatActivity.setNavBarColor(color: Int)
{
window.setNavigationBarColor(color)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
{
window.setNavigationBarDividerColor(color)
}
}
fun AppCompatActivity.setNavBarLightDark(is_light: Boolean)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
{
return
}
var flags = window.decorView.systemUiVisibility
if (is_light)
{
flags = flags and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
}
else
{
flags = flags or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
window.decorView.systemUiVisibility = flags
}
使用方法:
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
val color_status_bar = ContextCompat.getColor(this,R.color.yellow)
val color_nav_bar = ContextCompat.getColor(this,R.color.red)
this.setStatusBarColor(color_status_bar)
this.setStatusLightDark(false)
this.setNavBarColor(color_nav_bar)
this.setNavBarLightDark(true)
.....
}
<item name="android:windowLightNavigationBar">true</item>
在Oreo之前,浅色主题的支持不够完善。
这是我在API 24上以编程方式执行它的指令(当然它将从27+到33生效),使用Java(MainActivity):
WindowInsetsControllerCompat windowInsetsControllerCompat = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
windowInsetsControllerCompat.setAppearanceLightNavigationBars(true);
使用WindowInsetsControllerCompat,您还可以轻松地通过编程方式更改状态栏的亮度模式:
windowInsetsControllerCompat.setAppearanceLightStatusBars(true);
这个栏叫做 Snackbar,试试这个:
snackBarView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.BLACK));