我怎么能做出动作条上的项目是一个在左边,一个在市中心,一个在右边? [英] How can I make the items on ActionBar to be one on the left, one in the center and one on the right?

查看:145
本文介绍了我怎么能做出动作条上的项目是一个在左边,一个在市中心,一个在右边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 actionbarsherlock 做到这一点。 是我要在动作条例:
[登录]     企业LOGO]      [FILTER]

是我得到的动作条例:
            [登录]  企业LOGO]   [FILTER]

我在资源创建的登录按钮,公司标志和过滤器按钮(在可绘制的形式)/菜单的 activity_main.xml 。然而,在动作条的按键都无法完全转移到左边,尽管我已删除默认的应用程序标识,并设置那些

  getSupportActionBar()setDisplayShowTitleEnabled(假)。
getSupportActionBar()setDisplayShowHomeEnabled(假)。
 

下面是我的codeS在菜单中的 activity_main.xml

 <项目机器人:ID =@ + ID /登录
      机器人:图标=@可绘制/ login_btn
      机器人:layout_gravity =左
      机器人:showAsAction =总是/>

<项目机器人:ID =@ + ID /标志
      机器人:图标=@可绘制/ logo_btn
      机器人:layout_gravity =中心
      机器人:showAsAction =总是/>

<项目机器人:ID =@ + ID /过滤器
      机器人:图标=@可绘制/ filter_btn
      机器人:layout_gravity =右
      机器人:showAsAction =总是/>
 

解决方案

您应该创建的自定义视图。例如(布局/ ab_custom.xml ):

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =match_parent
    机器人:layout_gravity =fill_horizo​​ntal>

    <的ImageButton
        机器人:ID =@ + ID / BTN1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:SRC =@可绘制/左/>

    <的ImageButton
        机器人:ID =@ + ID / BTN2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerInParent =真
        机器人:SRC =@可绘制/中心/>

    <的ImageButton
        机器人:ID =@ + ID / btn3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:SRC =@可绘制/右/>

< / RelativeLayout的>
 

然后在您的活动的的onCreate 调用这个方法:

 私人无效showActionBar(){
        LayoutInflater充气=(LayoutInflater)本
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    视图V = inflator.inflate(R.layout.ab_custom,NULL);
    动作条动作条= getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(假);
    actionBar.setDisplayShowHomeEnabled(假);
    actionBar.setDisplayShowCustomEnabled(真正的);
    actionBar.setDisplayShowTitleEnabled(假);
    actionBar.setCustomView(五);
}
 

要采取控制你的项目使用此:

  @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例R.id.btn1:
            //左键,有所为有所不为
            返回true;
        案例R.id.btn2:
            //中央按钮
            返回true;
        案例R.id.btn3:
            //右边的按钮
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
        }
    }
 


编辑:修改后的我的答案。这是更好地使用 RelativeLayout的父

I'm using actionbarsherlock to do it. Example of what I want in the actionbar:
[LOGIN]     [COMPANY LOGO]     [FILTER]

Example of what I get in the actionbar:
      [LOGIN]  [COMPANY LOGO]  [FILTER]

I have created the login button, company logo and filter button (in the form of drawables) in the res/menu's activity_main.xml. However, those buttons on the actionbar are unable to shift fully to the left, even though I have removed the default application logo and set those to false:

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled (false);

here are my codes in the menu's 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"/>

解决方案

You should create custom view for that. For example (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>

Then in your Activity's onCreate call this method:

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);
}

To take control on your items use this:

 @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);
        }
    }


EDIT: revised my answer. It's better to use RelativeLayout as parent.

这篇关于我怎么能做出动作条上的项目是一个在左边,一个在市中心,一个在右边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆