我该如何制作一个位于中心具有标志的操作栏,两侧有两个操作项,像这样?
我将使用 ActionBar Sherlock。
我该如何制作一个位于中心具有标志的操作栏,两侧有两个操作项,像这样?
我将使用 ActionBar Sherlock。
我在评论中提到,我不确定任何这些内容是否会因为ABS而改变(我敢肯定不会有太大变化),但使用标准的操作栏,你可以为你的操作栏标题加载一个自定义的布局.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);
}
为了控制左侧的操作项,你可以在你的Activity中这样做:
@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中的其他必要操作项进行设置。这将适用于任何右侧操作项。<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>
<!-- or you could try @null -->
</style>