繁体   English   中英

Android Eclipse动作栏背景图片

[英]Android Eclipse Action Bar 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>

在布局文件夹中还包含一个“ 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>

我在菜单文件夹中有一个“ 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>

我在主类中的代码:

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);

结果看起来像这样:

测试结果

您使用的是Android 4.0或更高版本吗? 另外,也许使用福尔摩斯动作栏更容易

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

暂无
暂无

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

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