3

我正在使用 actionbarsherlock。用我的自定义主题

在 values/styles.xml 我已经输入了这个。

<style name="Mytheme" parent="@style/Theme.Sherlock.Light">
    <item name="android:background">@drawable/actionbar_bg</item>
</style>

actionbar_bg

<?xml version="1.0" encoding="utf-8"?>
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
    android:type="linear"
    android:centerX="8%" 
    android:startColor="#969696" 
    android:centerColor="#FFFFFF" 
    android:endColor="#FFFFFFFF" 
    android:angle="270"
/>

显现

<activity
        android:name="com.example.myapp.Secondactivity"
        android:label="@string/title_activity_second"
         android:theme="@style/Mytheme">
 </activity>

Logcat 错误 无法启动活动,android.view.InflateException: Binary XML file line #21: Error inflating class

当我删除android:theme="@style/Mytheme" 行时,该应用程序运行良好

更新: 我将 android:background 更改为“背景”,并且该应用程序现在不会强制关闭。但未应用渐变。我所能看到的只是一个灰色的操作栏。

更新#2

修改styles.xml如下

<style name="Mytheme" parent="@style/Theme.Sherlock.Light">
<item name="actionBarStyle">@style/MyActionBarTheme</item>
</style>

<style name="MyActionBarTheme" parent="Widget.Sherlock.ActionBar">
<item name="background">@drawable/actionbar_bg</item>
<item name="android:background">@drawable/actionbar_bg</item>
</style>

在 Manifest 中,我将主题称为 android:theme="@style/MyActionBarTheme" 。

正如您在图像中看到的那样,渐变分布在整个窗口中,而不仅仅是操作栏

在此处输入图像描述

4

2 回答 2

1

尝试不android使用您的 xml 属性名称:

<style name="Mytheme" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/Widget.MyActionBarTheme</item>
    <item name="android:actionBarStyle">@style/Widget.MyActionBarTheme</item>
</style>

<style name="Widget.MyActionBarTheme" parent="Widget.Sherlock.ActionBar">
    <item name="background">@drawable/actionbar_bg</item>
</style>

一个android将用于本机 ActionBar。

于 2013-04-23T11:18:32.270 回答
1

添加此 actionbar_bg.xml

在此处输入图像描述

Drawable dr = getResources().getDrawable(R.drawable.actionbar_bg);
getActionBar().setBackgroundDrawable(dr);

actionbar_bg.xml

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

    <gradient
        android:angle="270"
        android:centerColor="#FFFFFF"
        android:centerX="8%"
        android:endColor="#FFFFFFFF"
        android:startColor="#969696"
        android:type="linear" />

</shape>
于 2013-04-23T11:23:13.487 回答