如何在安卓中设置状态栏的颜色

3

如何在安卓中设置状态栏的颜色?

我已经尝试在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>

请查看此链接:https://dev59.com/VF4d5IYBdhLWcg3wLf4g#27093330 - Hardik Pithva
1
我觉得你不需要调用 getResources().getColor(Color.DKGRAY)),你可以直接传递 Color.DKGRAY。错误提示说要获取的是 Resource ID #0xff444444,但这是一个代表深灰色的十六进制值。 - Corey Ogburn
2个回答

5

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。这就是区别所在。
编辑:
您可以轻松地在主题中设置状态栏颜色。请注意,它仅在Android 5.0及以上版本上生效。在Android 4.4上,您可以使用半透明状态栏。如果您以这种方式操作,则不需要任何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>

在需要半透明状态栏的活动上使用AppTheme.TranslucentStatus


好的,谢谢..:) 那么在style.xml中设置状态栏颜色是否可行呢?在我发布的问题中,上面的代码只改变了操作栏文本的颜色,而没有改变状态栏的颜色。 - Karan

1

这个方法可用于API Level 21,因此您可能需要检查项目选项中配置的目标框架设置。

    And, you will need to include a check like this to verify 

这只在运行于Lollipop(API 21)或更高版本时执行:

                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                {
                    Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
                    Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
                    Window.SetStatusBarColor(Android.Graphics.Color.Blue);
                }

或者你可以尝试这个函数...

public void setStatusBarColor(@ColorRes int statusBarColor) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    int color = ContextCompat.getColor(this, statusBarColor);

    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(color);
  }
}

好的,谢谢..:) 那么在style.xml中设置状态栏颜色是否可行呢?在我提出问题时发布的上述代码中,它只改变了操作栏文本的颜色而没有改变状态栏的颜色。 - Karan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接