【问题标题】:how to set Drawer Layout icon in right side of the device screen?如何在设备屏幕右侧设置抽屉布局图标?
【发布时间】:2013-11-23 07:45:34
【问题描述】:

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

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

【问题讨论】:

标签: android android-listview drawerlayout


【解决方案1】:

也许为时已晚,但您可以使用默认菜单解决此问题。

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

然后在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);
}

接下来,在你的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;
        }
    };

最后别忘了在ActionBar 中隐藏主页按钮

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

【讨论】:

  • 这是正确的方法。但是您不再需要 ActionBarDrawerToggle。您可以直接从活动类中实现 onOptionsItemSelected。 onOptionsItemSelected 的代码将与您在此处显示的完全相同。
【解决方案2】:

如果您使用 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>

注意:android:layoutDirection="rtl"

【讨论】:

  • 它适用于抽屉图标。但我想把标题放在左边。左侧的标题和抽屉图标将在右侧。如何解决?
【解决方案3】:

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

但可以肯定的是,根据设计,这个代表导航的动画三行菜单图标应该在左侧。

仅供参考,要将导航抽屉放在右侧,您必须更改导航抽屉的重力,如下所示:

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

此外,在这种情况下,您真的很想要导航菜单图标,在右侧使用自定义标题布局或 ActionBarSherlock 之类的库来编辑它。

我希望这会有所帮助!

【讨论】:

【解决方案4】:

这是一个类似漏洞的解决方案,适用于我的案例。

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

我为它制作了一个工具栏。

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

并为其设置 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);
            }
        }

    });

【讨论】:

    【解决方案5】:

    来自Developer's Guide

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

    也就是说,您可以这样做:

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

    编辑

    根据Creating a Navigation Drawer

    抽屉视图(ListView)必须指定它的水平重力 使用 android:layout_gravity 属性。支持从右到左 (RTL)语言,用“start”而不是“left”指定值(所以 当布局为 RTL 时,抽屉出现在右侧)。

    所以你应该这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多