android动态设置状态栏颜色
时间: 2023-09-24 22:09:21 浏览: 110
你可以使用以下代码动态设置 Android 状态栏的颜色:
1. 首先,在你的 styles.xml 文件中创建一个新的样式:
```
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>
```
2. 然后,在你的 Activity 中,使用以下代码设置状态栏的颜色:
```
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
```
其中,`colorPrimaryDark` 是你想要设置的颜色值的资源 ID。如果你的应用程序需要支持旧版本的 Android,你还需要使用以下代码进行版本检查:
```
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
```
这将在 Android 5.0 及更高版本上设置状态栏颜色,并在 Android 4.4 及更高版本上设置半透明状态栏。
阅读全文
相关推荐














