3

如何在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>
4

2 回答 2

5

Color.DKGRAY已经是一个格式正确的颜色 int。你可以使用

getWindow().setStatusBarColor(Color.DKGRAY);

Resources.getColor()只有在解析颜色资源 ID时才需要使用,如下所示:

<resources>
    <color name="dark_gray">#ff444444</color>
</resources>

getWindow().setBackgroundColor(getResources.getColor(R.color.dark_gray));

在这种情况下,R.color.dark_gray不是颜色 int,而是资源 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>

AppTheme.TranslucentStatus在应具有半透明状态栏的活动上使用。

于 2016-04-01T14:29:40.630 回答
1

此方法在 API 级别 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);
  }
}
于 2016-04-01T14:36:43.247 回答