简体   繁体   English

Android Eclipse动作栏背景图片

[英]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: 我在values文件夹中有一个“ styles.xml” ,其中包含实际的背景图像:

<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: 在布局文件夹中还包含一个“ actionbarbg.xml” ,其中包含我提到的文本图像:

<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. 我在菜单文件夹中有一个“ actionbar.xml” ,该文件夹在操作栏上包含2个菜单按钮。

<?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? 您使用的是Android 4.0或更高版本吗? Also, maybe it's easier doing it with the sherlock actionbar 另外,也许使用福尔摩斯动作栏更容易

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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