ActionBar背景图片

rno*_*way 44 android android-3.0-honeycomb android-actionbar

我继承了Holo Light主题并使用以下内容自定义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>
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

图像被拉伸,而不是重复,任何想法为什么android:tileMode ="重复"不适用?

提前致谢

rno*_*way 43

好的,感谢Romain Guy在#android-dev IRC频道上,它是蜂窝/ 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);
Run Code Online (Sandbox Code Playgroud)

  • 我这样做了:final ActionBar actionBar = getActionBar(); BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(),R.raw.actionbar_background)); background.setTileModeX(android.graphics.Shader.TileMode.REPEAT); actionBar.setBackgroundDrawable(背景); (8认同)

Raj*_*edi 41

Drawable d=getResources().getDrawable(R.drawable.background_image_name);  
getActionBar().setBackgroundDrawable(d);
Run Code Online (Sandbox Code Playgroud)

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


Sat*_*jee 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>
Run Code Online (Sandbox Code Playgroud)

为此,您必须从"drawable"文件夹中选择一个图像.我选择图像"tp_black_bg.png"

之后不要忘记将此主题声明为AndroidManifest.xml文件

    <application
        .
        .
        .
        android:theme="@style/Theme.MyAppTheme" >.............</application>
Run Code Online (Sandbox Code Playgroud)

现在您可以重新打开任何XML布局文件,您可以轻松查看效果.以同样的方式,您还可以更改ActionBar的背景颜色.

谢谢.