如何在android中设置状态栏的颜色?
我在 styles.xml 和 .java 文件中都试过了。
如果我在 .java 类中尝试以下代码
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(Color.DKGRAY));
}
我收到异常调用:-ResourceNotFoundException
04-01 18:55:21.616: E/AndroidRuntime(2169): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.cz/com.myapp.casenotez.updateCase}: android.content.res.Resources$NotFoundException: Resource ID #0xff444444
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.ActivityThread.-wrap11(ActivityThread.java)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.os.Looper.loop(Looper.java:148)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.ActivityThread.main(ActivityThread.java:5417)
04-01 18:55:21.616: E/AndroidRuntime(2169): at java.lang.reflect.Method.invoke(Native Method)
04-01 18:55:21.616: E/AndroidRuntime(2169): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-01 18:55:21.616: E/AndroidRuntime(2169): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-01 18:55:21.616: E/AndroidRuntime(2169): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xff444444
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.content.res.Resources.getValue(Resources.java:1351)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.content.res.Resources.getColor(Resources.java:963)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.content.res.Resources.getColor(Resources.java:936)
04-01 18:55:21.616: E/AndroidRuntime(2169): at com.myapp.cz.updateCase.onCreate(updateCase.java:112)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.Activity.performCreate(Activity.java:6237)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
04-01 18:55:21.616: E/AndroidRuntime(2169): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
04-01 18:55:21.616: E/AndroidRuntime(2169): ... 9 more
我还尝试在 styles.xml 中添加样式:-
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#630</item>
<item name="android:titleTextStyle"> @style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/Blue</item>
</style>
Color.DKGRAY
已经是一个格式正确的颜色整数。你可以直接使用
getWindow().setStatusBarColor(Color.DKGRAY);
如果您要解析颜色资源 ID,则只需使用 Resources.getColor()
,如下所示:
<resources>
<color name="dark_gray">#ff444444</color>
</resources>
getWindow().setBackgroundColor(getResources.getColor(R.color.dark_gray));
在这种情况下,R.color.dark_gray
不是一个颜色整数,而是一个资源 ID。这就是区别。
编辑
您可以轻松地在主题中设置状态栏颜色。请注意,它仅对 Lollipop 及以上版本有效。您可以在 KitKat 上使用半透明状态栏。如果您这样做,则不需要任何 Java 代码来设置窗口标志。
请注意,我假设您使用的是 AppCompat 库。如果你不是,你可能应该。
在 res/values/styles.xml 中:
<!-- Extend from any AppCompat theme -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- put your theme customizations in here -->
</style>
<style name="AppTheme.TranslucentStatus" />
在 res/values-19/styles.xml 中:
<style name="AppTheme.TranslucentStatus">
<item name="android:windowTranslucentStatus">true</item>
</style>
在 res/values-21/styles.xml 中:
<style name="AppTheme.TranslucentStatus">
<item name="android:statusBarColor">#ff444444</item>
</style>
在应该有半透明状态栏的 Activity 上使用 AppTheme.TranslucentStatus
。
我是一名优秀的程序员,十分优秀!