简体   繁体   中英

how to set Drawer Layout icon in right side of the device screen?

I have created drawer layout sample application, it's working fine, my problem is drawer layout working in right to left perfectly but I am trying to move icon left side to right side but it's not working give me your suggestion..!!! This is possible or not?

在此输入图像描述

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

Maybe it's too late but you can solve this using the default Menu.

Create res/menu/my_right_side_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/btnMyMenu"
        android:icon="@drawable/ic_drawer"
        android:title="Right Side Menu"
        myapp:showAsAction="always"/>
</menu>

Then add your menu in onCreateOptionsMenu() in your Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    int menuToUse = R.menu.my_right_side_menu;

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(menuToUse, menu);

    return super.onCreateOptionsMenu(menu);
}

Next, in your ActionBarDrawerToggle handle the click event of your menu item

mDrawerToggle = new ActionBarDrawerToggle(this,  mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public boolean onOptionsItemSelected(android.view.MenuItem item) {
            if (item != null && item.getItemId() == R.id.btnMyMenu) {
                if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    mDrawerLayout.closeDrawer(Gravity.RIGHT);
                } else {
                    mDrawerLayout.openDrawer(Gravity.RIGHT);
                }
                return true;
            }
            return false;
        }
    };

And finally don't forget to hide your Home button from the ActionBar

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);

This icon represents navigation menu, which by design has to be on left side of the screen. As per the guidelines, we can although have a navigation drawer on right side, but that shall be used to modify the contents (for example filters). For all such purposes you might want to use ActionbarItem, and put up an ActionItem in right corner of the screen. Click on that action item will open or close the right navigation drawer.

But for sure, as per the design, this animated three lined menu icon, which represents navigation shall be on left hand side.

Just for the information, to put the navigation drawer on right side, you have to change the gravity of navigation drawer as follows:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_background" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
    <!-- The navigation drawer -->

    <LinearLayout
        android:id="@+id/right_drawer"
        android:layout_width="280dp"
        android:layout_gravity="end"
        android:layout_height="match_parent"
        android:orientation="vertical" />

</android.support.v4.widget.DrawerLayout>

Also, in this case you really really want the navigation menu icon, on right either use custom header layouts or a library like ActionBarSherlock to edit it.

I hope this helps!

If you use AppBarLayout and Toolbar do as below

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:layoutDirection="rtl">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />


</android.support.design.widget.AppBarLayout>

notice: android:layoutDirection="rtl"

Here is a loophole-like solution which worked in my case.

toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar0, R.string.open_nav, R.string.close_nav);

I made up a toolbar for it.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginRight="40dp"
            android:layout_marginEnd="40dp"
            android:background="@color/colorPrimary">
            <!-- this is your general toolbar-->
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="right|end|center_vertical"
                android:text="Mytitle"/>

        </android.support.v7.widget.Toolbar>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar0"
            android:layout_width="40dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="right|end"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            <!-- this is your nav-button toolbar-->

    </FrameLayout>

</android.support.design.widget.AppBarLayout>

and set onclicklistener for it:

toolbar0.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (drawer.isDrawerOpen(Gravity.RIGHT)) {
                drawer.closeDrawer(Gravity.RIGHT);
            } else {
                drawer.openDrawer(Gravity.RIGHT);
            }
        }

    });

You have two options if you want to don't have a toolbar or don't need a toolbar like in my case, there is the easy option of just adding an ImageButton in the right up corner of your activity like this

<android.support.v4.widget.DrawerLayout   
  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right" >

       <!-- The main content view -->
        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <FrameLayout
         android:id="@+id/content_frame"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         />  

        <ImageButton
            android:id="@+id/account_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="15dp"
            android:background="@null"
            android:src="@drawable/ic_menu_white_24dp" />

    </RelativeLayout>

<!-- The navigation drawer -->

<ListView
    android:id="@+id/drawer_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

and then simply add a listener on it and use the drawerLayout.isDrawerOpen() to Check for the current state of the drawer in a normal if condition, then opening and closing the drawer Using drawerLayout.openDrawer( ) and drawerLayout.closeDrawer(); and for each of the methods you can pass whether the gravity or the drawerView in you case is the ListView so it should be something like this drawer_layout.isDrawerOpen(drawer_list);

From Developer's Guide :

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)

Which means, you can do this by:

<DrawerLayout
    android:layout_gravity="right">
</DrawerLayout>

Edit

According to Creating a Navigation Drawer ,

The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravity attribute. To support right-to-left (RTL) languages, specify the value with "start" instead of "left" (so the drawer appears on the right when the layout is RTL).

So you should do:

<DrawerLayout
    android:layout_gravity="start">
</DrawerLayout>

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