44

我继承了 Holo Light Theme 并使用以下内容自定义了 ActionBar 的背景:

styles.xml 的内容

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/actionbar_background</item>
</style>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBar</item>
</style>
</resources>

actionbar_background.xml 的内容

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@raw/actionbar_background"
android:tileMode="repeat" />

图像没有被重复,而是被拉伸,知道为什么不应用 android:tileMode="repeat" 吗?

提前致谢

4

5 回答 5

43

好的,感谢#android-dev IRC 频道上的 Romain Guy,这是honeycomb / Android 3.0 上的一个已知错误,将在下一个版本中修复。从那时起,唯一的解决方案就是从代码中完成它,它可以工作:-)

 final ActionBar actionBar = getActionBar(); 
 BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background)); 
 background.setTileModeX(android.graphics.Shader.TileMode.REPEAT); 
 actionBar.setBackgroundDrawable(background);
于 2011-05-03T06:12:16.497 回答
43
Drawable d=getResources().getDrawable(R.drawable.background_image_name);  
getActionBar().setBackgroundDrawable(d);

上面的代码设置了操作栏的背景图片。
希望能帮助到你。

于 2012-12-07T07:40:00.653 回答
5

你可以很容易地做到这一点。如果您想更改操作栏背景图像,则将此代码放置到您的 res/styles.xml 文件中。

 <style name="Theme.MyAppTheme" parent="@android:style/Theme.Holo">
        <item name="android:actionBarStyle">@style/Theme.MyAppTheme.ActionBar</item>
    </style>

    <style name="Theme.MyAppTheme.ActionBar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:background">@drawable/top_black_bg</item>
    </style>

为此,您必须从“drawable”文件夹中选择一个图像。在这里我选择一个图像“tp_black_bg.png”

之后不要忘记将此主题声明到您的 AndroidManifest.xml 文件中

    <application
        .
        .
        .
        android:theme="@style/Theme.MyAppTheme" >.............</application>

现在您可以重新打开任何 XML 布局文件,您可以轻松看到效果。同样的方法你也可以改变ActionBar的背景颜色。

谢谢。

于 2013-12-31T11:26:35.647 回答
3
mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.navbar));
于 2015-03-10T02:09:40.090 回答
2

使用 android.support.v7 中的 getSupportActionBar() 以实现向后兼容性。

于 2015-01-23T11:03:22.773 回答