9

我正在使用Action Bar Compat,以便我的导航抽屉的操作栏向后兼容到API级别9,并且我想更改操作栏的背景。ActionBar的自定义(渐变)背景Compat

我从Android Developers复制代码:

<?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样子:

<?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

应该如何绘制背景?

回答

16

我目前正在进行相同的任务。

这是我的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/turquoise_action_bar" 
     android:endColor="@color/dark_turquoise" 
     android:startColor="@color/dark_turquoise" /> 
</shape> 

DeadObjectException

android:shape="line"如果存在内部梯度,不能使用。我测试了它;我的三星Galaxy Note 10.1 N8000重新启动,并且有一个DeadObjectException

linear类型的梯度模式是默认值。所以你不必明确地声明它。

这里是我的styles.xml中的文件夹。

<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> 

+1

<项目名称=“机器人:actionBarStyle”> @风格/ PeopleCanAct应改为<项目名称=“机器人:actionBarStyle”> @风格/ MyActionBar你会得到一个编译错误的,是 –

+0

这工作,尽管我不明白DeadObjectException部分,但设备何时会得到该异常? – Bruce

16

另一种方法,而无需修改styles.xml

这里是我们的榜样GradientDrawable在res/drawable/ab_gradient.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
    android:useLevel="false" > 

    <gradient 
     android:angle="90" 
     android:startColor="#000000" 
     android:endColor="#FFFFFF" 
     android:type="linear" /> 

</shape> 

你可以将其设置为操作栏在你活动的onCreate()

ActionBar actionBar = getActionBar();  
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape)); 

如果使用支持V7库(你的情况):

// make sure to import android.support.v7.app.ActionBar 
ActionBar actionBar = getSupportActionBar();   
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape)); 

或者,如果你使用ActionBarSherlock:

// make sure to import com.actionbarsherlock.app.ActionBar 
ActionBar actionBar = getSherlock().getActionBar();  
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape)); 
+0

这显然也有效,但我接受了其他答案,因为它解释了我的代码出了什么问题。 –

+0

@ Max77 .getDrawable现在已弃用。什么是代替弃用方法的新代码? – iOSAndroidWindowsMobileAppsDev

+1

@JqueryNinja检查这个很好的答案:http://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22 – Max77

0

我也使用来自Android开发者是示例代码和使用渐变XML和你的一样。

<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="line"> 

我发现你和我之间的文件不同的是,android:shape="line"/android:shape="rectangle"

所以我尝试改变我的矩形线。我的应用程序也出现相同的异常,并且操作系统重新启动。也许形状是关键。

+0

这并没有真正回答这个问题。如果您有不同的问题,可以通过单击[提问](http://stackoverflow.com/questions/ask)来提问。您还可以[添加赏金](http://stackoverflow.com/help/privileges/set-bounties)在您拥有足够的[声誉](http://stackoverflow.com/help/)时吸引更多人关注此问题什么声誉)。 – maszter