简体   繁体   中英

How to align menu item in ActionBar

Hi I have three menu items that are getting displayed on right side by default.

I want to show one Menu on Left most and other two on right.

my Menu.xml file is as

   <menu xmlns:android="http://schemas.android.com/apk/res/android" >

   <item
    android:id="@+id/action_version"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_version"/>
   <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
   <item
    android:id="@+id/action_refresh"
    android:actionViewClass="android.support.v7.widget.RefreshView"
    android:icon="@drawable/ic_refresh_holo_dark"
    android:orderInCategory="100"
    android:gravity ="left"
    android:layout_gravity="left"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="Refresh"/>

   </menu>

How can I align refresh menu on left most and others on right only.

So finally I did as.

My custom Action Bar xml file as

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="fill_horizontal"
android:background="@color/grey"
android:orientation="horizontal" >

<ImageButton
    android:id="@+id/imgrefresh"
    android:layout_width="40dp"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"

    android:layout_marginLeft="10dp"
    android:onClick="toggleMenu"
    android:background="@drawable/img_refresh_click"
    android:scaleType="centerInside"
    android:src="@drawable/ic_refresh_holo_dark" />

<CustomEditText
    android:id="@+id/txtSearchURL"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="0dp"
    android:layout_toRightOf="@+id/imgrefresh"
    android:background="@drawable/url_background"
    android:drawableRight="@drawable/ic_stop_holo_dark"
    android:focusableInTouchMode="true"
    android:hint="@string/search_url_hint"
    android:imeOptions="actionGo"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:textColor="@color/white" />

Where img_refresh_click indrawable is as

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"><shape>
        <gradient android:angle="270" android:endColor="#33819F" android:startColor="#33819F" />
    </shape></item>

<item><shape>
        <gradient android:angle="270" android:endColor="@android:color/transparent" android:startColor="@android:color/transparent" />


    </shape></item>

Now inside onCreate() method

    ActionBar actionBar = getActionBar();
    getActionBar().setDisplayShowHomeEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setBackgroundDrawable(new ColorDrawable(0xff353538));
    // actionBar.setIcon(R.drawable.ic_action_search);

    LayoutInflater inflator = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflator.inflate(R.layout.header, null);
    actionBar.setCustomView(v);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

and My screen becomes as I expected

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM