簡體   English   中英

如何在android中為狀態欄設置顏色

[英]How can I set color for statusbar in android

如何在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已經是一個正確形成的顏色int。 你可以使用

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不是一個顏色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

此方法在API Level 21中可用,因此您可能希望在項目選項中檢查Target Framework設置的配置。

    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);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM