动作条的背景图像

问题描述:

我继承了Holo浅色主题和个性化的动作条与背景中的以下内容:actionbar_background.xml的动作条的背景图像

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> 

内容

<?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 =“重复”没有应用?

在此先感谢

好,感谢罗曼盖伊上#Android的开发IRC频道,它是在蜂窝一个已知的bug/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); 
+1

请问你是如何从代码中完成的?如何设置为tileMode重复?当我尝试获取动作栏时,我总是得到空值 – Alex 2011-05-12 13:41:47

+8

我这样做了: 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); – rnoway 2011-05-13 06:17:44

+1

@rnoway,最好在你的回答中添加你的评论作为编辑。您应该将答案标记为解决方案,因为它可以解决问题。 – 2013-06-21 21:27:39

Drawable d=getResources().getDrawable(R.drawable.background_image_name); 
getActionBar().setBackgroundDrawable(d); 

上述代码设置的背景图像的操作栏。
希望它有帮助。

+0

但为什么它不支持以下版本如2.2,2.3.3等等 – prabu 2014-05-22 11:59:01

你可以很容易地做到这一点。如果您想更改Action Bar背景图片,请将此代码放到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> 

对于这一点,你必须选择“绘制”文件夹中的图像。在这里,我选择的图像“tp_black_bg.png”

之后不要忘了这个主题声明AndroidManifest.xml档案

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

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

谢谢。

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

mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.navbar));