【问题标题】:Is there a way to set actionbar backgound by image in android?有没有办法在android中按图像设置动作栏背景?
【发布时间】:2012-12-15 18:14:32
【问题描述】:

我需要将操作栏的背景更改为自定义图像,但每次我尝试使用此代码时都不会改变任何东西。

Bitmap b =  BitmapFactory.decodeResource(getResources(), R.drawable.navbarbg);
BitmapDrawable bd = new BitmapDrawable(getResources(), b);
bar.setBackgroundDrawable(bd);

我也试过这段代码,但没有用。

Resources res = getResources();
xpp =res.getXml(R.drawable.actionbar_background);
bitmapDrawable = (BitmapDrawable) BitmapDrawable.createFromXml (res, xpp);

actionbar_background.xml的内容是

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

编辑: 我使用了这段代码,效果很好:

Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.action_bar_bg);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(actionBarBackground);

【问题讨论】:

    标签: android background bitmap android-actionbar bitmapfactory


    【解决方案1】:
    getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.navbarbg));
    

    【讨论】:

      【解决方案2】:

      我使用了这段代码,效果很好:

      Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.action_bar_bg);
      BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
      ActionBar bar = getActionBar();
      bar.setBackgroundDrawable(actionBarBackground);
      

      【讨论】:

        【解决方案3】:

        您可以通过玩styles.xml. 来做到这一点这里有一个详细的explanation。查看标记的答案。它是为 actionbarsherlock 解释的。但是方法是一样的。如果您需要更多详细信息。请参考这个link,它也是由其他回答者给出的。

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
            <!-- the theme applied to the application or activity -->
            <style name="MyTheme" parent="@android:style/Theme.Holo">
                <!-- Here you are including your actual custom theme in which your own things are defined --> 
                <item name="android:actionBarStyle">@style/MyActionBar</item>
                <!-- other activity and action bar styles here -->
            </style>
        
            <!-- style for the action bar background which is named as "MyActionBar. The same name is being used in the above style." -->
            <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> 
                <item name="android:background">@drawable/actionbar_background</item>
            </style>
        </resources> 
        

        不要忘记在清单文件中包含这个自定义主题,如下所示,以便在整个应用程序中生效。

        <application android:theme="@style/MyTheme" />
        

        如果您希望它仅用于特定活动,您甚至可以将这个主题包含在活动中。希望这会有所帮助。

        【讨论】:

        • 整个应用程序和整个 listView 具有相同的背景,这让一切看起来都很糟糕,我也尝试在操作栏上使用它,但没有任何反应。还尝试将样式添加到活动中,活动中的每一件事都有无法工作的背景。
        • 你可能走错了地方。因为它有效,而且我让它在我的项目中有效。如果您仍然不明白,请使用您的代码编辑您的问题。
        猜你喜欢
        • 2013-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多