ActionBar Compat的自定义(渐变)背景 [英] Custom (gradient) background of ActionBar Compat
问题描述
我正在使用Action Bar Compat,以便带导航抽屉的操作栏向下兼容到API级别9,并且我想更改操作栏的背景.
I am using Action Bar Compat so that my action bar with navigation drawer was backward compatible down to API level 9 and I want to change the background of the action bar.
我从 Android开发人员中复制了代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_background</item>
</style>
</resources>
问题来了.
当我将可绘制图像或颜色作为背景时,它可以正常工作.但是我想将背景定义为渐变形状,所以我的actionbar_background
看起来像:
When I put an image drawable or a color as the background, it works fine. However I want to define the background as a gradient shape, so my actionbar_background
looks like:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<gradient
android:startColor="@color/ac_bg_start"
android:endColor="@color/ac_bg_end"
android:type="linear"/>
<size
android:width="1dp"
android:height="48dp"/>
</shape>
我希望以水平方式重复它,但是即使这样也会导致错误,实际上是非常有趣的错误.当我尝试运行应用程序时,测试设备甚至模拟器都会重新启动.我能够在重新启动之前捕捉到DeadObjectException
.
I want it to be repeated in horizontal way but even this results in error, in fact, very interesting error. Test device and even the emulator gets restarted when I try to run the app. I was able to catch DeadObjectException
before restarting.
背景可绘制的外观如何?
推荐答案
我目前正在从事同一任务.
I am currently working on the same task.
这是我的 action_bar_bg.xml 文件,其中定义了操作栏的渐变.
Here is my action_bar_bg.xml file in which I define the gradient for my action bar.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="@color/turquoise_action_bar"
android:endColor="@color/dark_turquoise"
android:startColor="@color/dark_turquoise" />
</shape>
DeadObjectException
android:shape="line"
.我测试过了我的三星Galaxy Note 10.1 N8000重新启动,并且出现了DeadObjectException
.
android:shape="line"
can't be used if there is a gradient inside. I tested it; my Samsung Galaxy Note 10.1 N8000 restarted, and there was a DeadObjectException
.
渐变样式的linear
类型是默认值.因此,您不必显式声明它.
The linear
type of gradient pattern is the default value. So you don't have to declare it explicitly.
这是我的styles.xml
在 values 文件夹中.
<resources>
<!-- Base application theme. -->
<style name="AppThemeBase" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/PeopleCanAct</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="AppTheme" parent="AppThemeBase">
<!-- All the customizations that are NOT specific to a particular API level can go here -->
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/action_bar_bg</item>
<!-- Support library compatibility -->
<item name="background">@drawable/action_bar_bg</item>
</style>
</resources>
这篇关于ActionBar Compat的自定义(渐变)背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!