【问题标题】:ActionBar logo centered and Action items on sidesActionBar 徽标居中,Action 项目位于两侧
【发布时间】:2012-09-26 19:57:54
【问题描述】:

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

我将使用 ActionBar Sherlock。

【问题讨论】:

    标签: android android-actionbar center graphical-logo


    【解决方案1】:

    正如我在评论中指出的那样,我不确定这是否会随着 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>
    

    【讨论】:

    • 所以左侧操作项的解决方案是使用 UP 按钮来伪造它,就像我在第一条评论中写的那样,对吧?
    • 是的,你会想为 android:homeAsUpIndicator 添加一个自定义样式,这样你就不会得到那个左 V 形指示器。我会更新更多代码。
    • 如何在没有 android:homeAsUpIndicator 的情况下隐藏箭头?它需要 API 级别 11,我的最低要求是 8
    • 你在使用 ActionBarSherlock 吗?
    • 实际上我们应该创建自己的 MyActivity 扩展 Activity 并在那里做一次。
    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 2018-12-05
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多