Android ActionBar 徽标居中,Action 项位于两侧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12750013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionBar logo centered and Action items on sides
提问by urSus
How could I make an actionbar to have logo in the center and two action items on sides like this?
我怎样才能制作一个动作栏,使其在中心有标志,在两侧有两个动作项目?
I will be using ActionBar Sherlock.
我将使用 ActionBar Sherlock。
回答by dennisdrew
As I noted in a comment, I am not exactly sure if any of this would change with ABS (I'm sure not much would), but with the standard Action Bar you can load a custom layout .xml for your action bar title. For example, you could have action_bar_title.xml:
正如我在评论中指出的那样,我不确定这是否会随着 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>
Then, in your Activity, you would want to call a method like this in onCreate():
然后,在您的 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);
}
To take control of the left action item, you could do this in your Activity:
要控制左侧的操作项,您可以在 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);
}
}
Of course, make sure you also do the other necessary setups for the Action items in your menu xml's as well. This will be for any right-sided action items.
当然,请确保您还对菜单 xml 中的操作项进行了其他必要的设置。这将适用于任何右侧的操作项。
Edit: Just saw in another comment you said that title would be an image. If that's the case, just replace the TextView I used in my example with your ImageView, and disregard the font customization code in the setupActionBar() method.
编辑:刚刚在另一条评论中看到你说标题是一张图片。如果是这种情况,只需将我在示例中使用的 TextView 替换为您的 ImageView,并忽略 setupActionBar() 方法中的字体自定义代码。
Edit2: I updated my Java code. You will need to do ab.setHomeAsUpEnabled(true), and then have a custom theme to implement an invisible (or perhaps @null although I don't know if Android will accept that) drawable for the home as up indicator. See here (taken from answer here:)
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>