如何让ActionBar上的项目在左侧,一个在中间,一个在右侧?

ah *_*gal 13 android actionbarsherlock

我正在使用actionbarsherlock它.我在动作栏中想要的示例:
[登录] [公司徽标] [过滤器]

我在操作栏中获得的示例:
      [登录] [公司徽标] [过滤器]

我在res/menu中创建了登录按钮,公司徽标和过滤按钮(以drawables的形式)activity_main.xml.但是,操作栏上的那些按钮无法完全向左移动,即使我已删除默认应用程序徽标并将其设置为false:

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled (false);
Run Code Online (Sandbox Code Playgroud)

这是菜单中的代码activity_main.xml:

<item android:id="@+id/login"
      android:icon="@drawable/login_btn"
      android:layout_gravity="left"
      android:showAsAction="always"/>

<item android:id="@+id/logo"
      android:icon="@drawable/logo_btn"
      android:layout_gravity="center"
      android:showAsAction="always"/>

<item android:id="@+id/filter"
      android:icon="@drawable/filter_btn"
      android:layout_gravity="right"
      android:showAsAction="always"/>
Run Code Online (Sandbox Code Playgroud)

Sab*_*bre 32

您应该为此创建自定义视图.例如(layout/ab_custom.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal" >

    <ImageButton
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/left" />

    <ImageButton
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/center" />

    <ImageButton
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/right" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

然后在你的Activity中onCreate调用这个方法:

private void showActionBar() {
        LayoutInflater inflator = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.ab_custom, null);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled (false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setCustomView(v);
}
Run Code Online (Sandbox Code Playgroud)

要控制您的物品,请使用以下命令:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.btn1:
            //left button, do something
            return true;
        case R.id.btn2:
            //center button
            return true;
        case R.id.btn3:
            // right button
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑:修改我的答案.最好使用RelativeLayout as parent.

  • 事实上,正确的膨胀方式是:`getSupportActionBar().setCustomView(R.layout.ab_custom);` (2认同)
  • 像这样向自定义视图添加按钮不会将它们连接到ActionBar的选项选择机制,因此单击按钮时不会调用onOptionsItemSelected。我尝试过手动添加onClickListener,但从未被调用过。有没有解决的办法? (2认同)