![](/img/trans.png)
[英]How to change the color of the status bar in a codenameone android app?
[英]Change android colorPrimary (app bar) to gradient color
我需要将应用程序栏上的颜色更改为某种渐变颜色。 我在drawable文件夹中创建了action_bar_bg.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="@color/colorPrimaryDark"
android:endColor="@color/colorPrimary"
android:startColor="@color/colorPrimary" />
</shape>
我试图在styles.xml中更改colorPrimary:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@drawable/action_bar_bg</item>
</style>
但它不起作用。 我收到错误: android.content.res.Resources$NotFoundException: File res/drawable/action_bar_bg.xml from color state list resource ID #0x7f020046)
我想实现这个结果:
我需要在代码中更改什么?
试试这种方式。 有一个名为colorPrimary的新属性,您可以在主题中定义该属性。 这将为ActionBar或工具栏提供纯色。
举个小例子:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/my_action_bar_color</item>
</style>
请注意:除了值-v21之外,每个values-folder中必须只有colorPrimary,而不是android:colorPrimary。
您可以阅读有关在Android上自定义调色板的更多信息
在您的活动中的onCreate中执行此操作
ActionBar bar = getActionBar();
Drawable gradient = getResources().getDrawable( R.drawable.action_bar_bg);
bar.setBackgroundDrawable(gradient);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.