简体   繁体   中英

Setting a background drawabe for actionbar in android

I need to set different background image for action bar for different fragments. I am using the v4 support package. But I am getting a white background instead of the given image.

Below is the code I have used.

getActivity().getActionBar().setBackgroundDrawable(getActivity().getResources().getDrawable(R.id.image));

and also

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        BitmapDrawable actionBarBackground = new BitmapDrawable(getResources(), bMap);
        ActionBar bar = getActivity().getActionBar();
        bar.setBackgroundDrawable(actionBarBackground);

I have created this style and used it in the manifest for the activity

android:theme="@style/ActionBarTheme"

 <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
 <item name="android:background">@drawable/image</item>
 </style>

 <style name="ActionBarTheme" parent="@android:style/Theme.Holo.Light">
 <item name="android:actionBarStyle">@style/MyActionBar</item>
 </style>

试试这个

getActivity().getActionBar().setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.image));
<style name="CustomActivityTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <!-- other activity and action bar styles here -->
    </style>

    <!-- style for the action bar backgrounds -->
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>

In your manifest file:

 <activity android:name=".activity"
                  android:theme="@style/CustomActivityTheme"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM