ActionBar标志居中,双方的行动项目

我怎么能做一个行动栏,在这个中心有两个标志?

在这里输入图像描述

我将使用ActionBar Sherlock。

正如我在评论中指出的那样,我不确定这样做是否会随着ABS而改变(我确定不会太多),但是使用标准的Action Bar,您可以为您的操作栏标题加载自定义布局.xml。 例如,你可以有action_bar_title.xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/your_desired_background" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:maxLines="1" android:ellipsize="end" android:text="@string/app_name" android:textAppearance="?android:attr/textAppearanceMedium"/> </RelativeLayout> 

然后,在你的Activity中,你需要在onCreate()中调用这样的方法:

 private void setupActionBar() { ActionBar ab = getActionBar(); ab.setDisplayShowCustomEnabled(true); ab.setDisplayShowTitleEnabled(false); ab.setIcon(R.drawable.your_left_action_icon); LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(R.layout.action_bar_title, null); TextView titleTV = (TextView) v.findViewById(R.id.title); Typeface font = Typeface.createFromAsset(getAssets(), "fonts/your_custom_font.ttf"); titleTV.setTypeface(font); ab.setCustomView(v); ab.setHomeAsUpEnabled(true); } 

要控制左侧的操作项目,您可以在“活动”中执行此操作:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.yourRightActionItem: //do something return true; case android.R.id.home: //<-- this will be your left action item // do something return true; default: return super.onOptionsItemSelected(item); } } 

当然,确保你也为菜单xml中的Action项目做了其他必要的设置。 这将是任何右侧的行动项目。

编辑:只是在另一个评论中看到你说,标题将是一个形象。 如果是这样的话,只需将我的示例中使用的TextViewreplace为ImageView,并忽略setupActionBar()方法中的字体自定义代码。

Edit2:我更新了我的Java代码。 你将需要做ab.setHomeAsUpEnabled(true),然后有一个自定义的主题来实现一个隐形的(或者@null,虽然我不知道Android是否会接受),作为向上指标家庭可绘制。 看到这里(从这里回答:)

 <style name="Theme.MyFancyTheme" parent="android:Theme.Holo"> <item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item> <!-- or you could try @null --> </style>