简体   繁体   中英

Android Eclipse Action Bar Background Image

I would like to add my own image background to the action bar. How can I do that? Also on top of this on the left I would like to add another image of my own which contains a text. Is this possible?

I haven't managed to find any examples of this at all.

I have got a "styles.xml" , in values folder, which contains the actual background image:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="AppTheme" parent="android:Theme.Holo" />

<style name="ActionBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">45dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@drawable/actionbar_bg</item>
</style>

</resources>

I've also got an "actionbarbg.xml" , within the layout folder, which contains the text image I mentioned:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/ActionBar" >

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:paddingLeft="15dip"
android:scaleType="center"
android:src="@drawable/bannerlogo" />
/>

</LinearLayout>

I have an "actionbar.xml" , within the menu folder, which contains 2 menu buttons on the action bar.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/item_refresh"
android:icon="@drawable/help"
android:title="Refresh"
android:showAsAction="ifRoom|withText" />

<item
android:id="@+id/item_save"
android:icon="@drawable/info"
android:title="Save"
android:showAsAction="ifRoom|withText" />

</menu>

My code within my main class:

ActionBar actionBar = getActionBar();

actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);

View mCustomView = mInflater.inflate(R.layout.actionbarbg, null);

actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);

The result looks like this:

测试结果

Are you using Android 4.0 or higher? Also, maybe it's easier doing it with the sherlock actionbar

也许您可以根据需要尝试设置android:src和android:background。

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