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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20159934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:23:49  来源:igfitidea点击:

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

androidandroid-listviewdrawerlayout

提问by balaji

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?

我已经创建了抽屉布局示例应用程序,它工作正常,我的问题是抽屉布局从右到左完美工作,但我试图将图标从左到右移动,但它不起作用给我你的建议..!!! 这可能还是不可能?

enter image description here

在此处输入图片说明

<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>

采纳答案by Vivek Soneja

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.

此图标代表导航菜单,按设计必须位于屏幕左侧。根据指南,我们虽然可以在右侧有一个导航抽屉,但应用于修改内容(例如过滤器)。对于所有这些目的,您可能想要使用 ActionbarItem,并在屏幕的右上角放置一个 ActionItem。单击该操作项将打开或关闭右侧的导航抽屉。

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!

我希望这有帮助!

回答by Nando

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

然后添加你的菜单中onCreateOptionsMenu()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 ActionBarDrawerTogglehandle the click event of your menu item

接下来,在您的ActionBarDrawerToggle句柄中处理菜单项的点击事件

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

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

回答by farhang67

If you use AppBarLayout and Toolbar do as below

如果您使用 AppBarLayout 和 Toolbar,请执行以下操作

<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"

回答by navid

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

    });

回答by Mohamed Hajr

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 ImageButtonin 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();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);

然后简单地在其上添加一个侦听器并使用drawerLayout.isDrawerOpen()在正常 if 条件下检查抽屉的当前状态,然后使用drawerLayout.openDrawer() 和drawerLayout.closeDrawer()打开和关闭抽屉并且对于每种方法,您都可以传递重力或 drawerView 在您的情况下是 ListView,因此它应该是这样的 drawer_layout.isDrawerOpen(drawer_list);

回答by chinglun

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.)

抽屉的定位和布局是使用 android:layout_gravity 属性控制的,子视图对应于您希望抽屉出现的视图的哪一侧:左侧或右侧。(或在支持布局方向的平台版本上开始/结束。)

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).

抽屉视图(ListView)必须使用 android:layout_gravity 属性指定其水平重力。要支持从右到左 (RTL) 语言,请使用“开始”而不是“左”指定值(因此当布局为 RTL 时,抽屉显示在右侧)。

So you should do:

所以你应该这样做:

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