【问题标题】:ActionBar background image动作栏背景图片
【发布时间】:2011-08-17 05:45:45
【问题描述】:

我继承了 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" 吗?

提前致谢

【问题讨论】:

    标签: android android-3.0-honeycomb android-actionbar


    【解决方案1】:
    Drawable d=getResources().getDrawable(R.drawable.background_image_name);  
    getActionBar().setBackgroundDrawable(d);
    

    上面的代码设置了操作栏的背景图片。
    希望对您有所帮助。

    【讨论】:

    • 为什么不支持2.2、2.3.3等以下版本
    【解决方案2】:

    好的,感谢#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);
    

    【讨论】:

    • 请问您是如何通过代码实现的?如何设置为 tileMode 重复?尝试获取操作栏时我总是得到 null
    • 我是这样做的:final ActionBar actionBar = getActionBar(); BitmapDrawable 背景 = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background));背景.setTileModeX(android.graphics.Shader.TileMode.REPEAT); actionBar.setBackgroundDrawable(背景);
    • @rnoway ,最好将您的评论添加为答案中的编辑。您应该将答案标记为解决方案,因为它可以解决问题。
    • 对我来说,如果我在 setBackgroundDrawable 之后调用 invalidateMenuOptions 而不对 Drawable 的类型做任何假设,它就可以工作
    • @rnoway:为什么要定义 actionBar 属性 final?最终定义它如何影响它?对不起,我是 Java 和 Android 的新手。谢谢
    【解决方案3】:

    你可以很容易地做到这一点。如果您想更改操作栏背景图像,则将此代码放置到 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的背景颜色。

    谢谢。

    【讨论】:

      【解决方案4】:
      mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.navbar));
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-26
          • 2020-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-04
          • 1970-01-01
          相关资源
          最近更新 更多