简体   繁体   English

如何在设备屏幕的右侧设置抽屉布局图标?

[英]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 创建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 然后在Activity中的onCreateOptionsMenu()中添加菜单

@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 接下来,在ActionBarDrawerToggle处理菜单项的click事件

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 最后不要忘记隐藏ActionBar Home按钮

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. 出于所有这些目的,您可能希望使用ActionbarItem,并在屏幕的右上角放置一个ActionItem。 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. 此外,在这种情况下,您真的非常希望右侧的导航菜单图标使用自定义标题布局或ActionBarSherlock等库来编辑它。

I hope this helps! 我希望这有帮助!

If you use AppBarLayout and Toolbar do as below 如果您使用AppBarLayout和工具栏,请执行以下操作

<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" 注意: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: 并为它设置onclicklistener:

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 如果您不想使用工具栏或者不需要像我这样的工具栏,您有两种选择,只需在活动的右上角添加ImageButton就可以了

<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(); 然后简单地在其上添加一个监听器并使用drawerLayout.isDrawerOpen()来检查正常if条件下抽屉的当前状态,然后打开和关闭抽屉使用drawerLayout.openDrawer( )和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); 对于你可以传递的每个方法,无论你的情况下的引力还是抽象视图都是ListView,所以它应该像这样的抽屉_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. 使用子视图上的android:layout_gravity属性控制抽屉定位和布局,该属性对应于您希望抽屉出现的视图的哪一侧:左侧或右侧。 (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. 抽屉视图(ListView)必须使用android:layout_gravity属性指定其水平重力。 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). 要支持从右到左(RTL)语言,请使用“start”而不是“left”指定值(因此当布局为RTL时,抽屉将显示在右侧)。

So you should do: 所以你应该这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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