android - ActionBar Logo 居中,Action 项目位于两侧

标签 android android-actionbar center graphical-logo

我怎样才能使操作栏在中心有 Logo ,在两侧有两个操作项?

enter image description here

我将使用 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 中的操作项进行了其他必要的设置。这将适用于任何右侧的操作项。

编辑:刚刚在另一条评论中看到您说标题将是一张图片。如果是这种情况,只需将我在示例中使用的 TextView 替换为您的 ImageView,并忽略 setupActionBar() 方法中的字体自定义代码。

Edit2:我更新了我的 Java 代码。您将需要执行 ab.setHomeAsUpEnabled(true),然后有一个自定义主题来实现一个不可见的(或者可能是 @null,尽管我不知道 Android 是否会接受)可绘制的主页作为向上指示器。见这里(取自答案 here :)

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

关于android - ActionBar Logo 居中,Action 项目位于两侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12750013/

相关文章:

android - getDrawable() 内存不足错误

android - MenuItem 的选中状态未通过其图标正确显示

html - CSS:单个 <td> 的不同垂直对齐方式

java - 显示错误 "Supertypes of the following classes cannot be resolved.",没有任何类列表

android - 使用maven发布找不到传递依赖

java - 某些设备上的字符 0 错误输入结束

android - 如果 API 为 11 或更高,如何检查设备的 API 级别并使用 Actionbar?

android - 仅在操作模式 (CAB) 中拆分操作栏

html - DIV的水平中心不是那么简单

css - 文字不能居中?