如何在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.
标签:android-statusbar,android,android-styles 来源: https://codeday.me/bug/20190829/1759171.html