Android - 可以从右侧导航抽屉吗?

问题描述 投票:0回答:11

http://developer.android.com/training/implementing-navigation/nav-drawer.html

根据此文档,它没有说明是否可以从右侧实现抽屉。有可能吗? :(

android navigation-drawer
11个回答
69
投票

NavigationDrawer 可以配置为从左侧、右侧或两者拉出。关键是 XML 声明中抽屉的出现顺序以及layout_gravity 属性。这是一个例子:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >
    </FrameLayout>

    <!-- Left drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice" />

    <!-- Right drawer -->

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>

48
投票

这是抽屉上的文档,看来您可以将其配置为从左侧或右侧拉出。

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

http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html


25
投票

我的应用程序崩溃,并出现“重力向左未找到抽屉视图”错误。

因此将其添加到 onOptionsItemSelected 中:

if (item != null && item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            mDrawerLayout.closeDrawer(Gravity.RIGHT);
        } else {
            mDrawerLayout.openDrawer(Gravity.RIGHT);
        }
    }

5
投票

添加到 https://stackoverflow.com/a/21781710/437039 解决方案。

如果您使用的是 Android Studio 创建的导航抽屉项目,那么情况将会发生变化

onOptionsItemSelected
。由于他们创建了子类,因此您必须使用此代码

if (item != null && id == android.R.id.home) {
        if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
            mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
        } else {
            mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
        }
        return true;
}

接下来。在类

NavigationDrawerFragment
中,您必须创建 3 个方法:

方法一

public boolean isDrawerOpen(int gravity) {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(gravity);
}

方法2

public void closeDrawer(int gravity) {
    mDrawerLayout.closeDrawer(gravity);
}

方法3

public void openDrawer(int gravity) {
    mDrawerLayout.openDrawer(gravity);
}

只有现在,右侧抽屉才能发挥作用。


3
投票

您可以使用 Material design 中的

NavigationView
。对于前:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

1
投票

我知道这是一个老问题,但对于那些仍在寻找答案的人来说:

是的,这是可能的。请在下面的链接上查看我的答案:

https://stackoverflow.com/a/19358114/1572408


1
投票

然后使用这些代码@amal 我认为这会对您有所帮助。 XML:

<!-- Framelayout to display Fragments -->

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/counter_bg" >

        <ImageView
            android:id="@+id/iconl"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/iconr"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="17dp"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>
</FrameLayout>

<!-- Listview to display slider menu -->

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

<ListView
    android:id="@+id/list_slidermenu2"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

//设置需要的图片

活动代码:

ImageView iconl,iconr;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList,mDrawerList2;

ImageView iconl,iconr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iconl = (ImageView)findViewById(R.id.iconl);
    iconr = (ImageView)findViewById(R.id.iconr);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
    mDrawerList2 = (ListView) findViewById(R.id.list_slidermenu2);
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
    mDrawerList2.setOnItemClickListener(new SlideMenuClickListener());
    iconl.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.START);
            mDrawerLayout.closeDrawer(Gravity.END);
        }
    });
    iconr.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDrawerLayout.openDrawer(Gravity.END);
            mDrawerLayout.closeDrawer(Gravity.START);
        }
    });
}
}

在这里,您可以为两个列表设置自己的列表适配器,并在项目上单击调用 displayView(position);您可以将片段添加到框架布局的方法。

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;


    default:
        break;
    }

    if (fragment != null) 
    {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
        .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);

        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

1
投票

要从屏幕右侧设置导航抽屉,请将抽屉布局设置为导航视图的父级,并将导航视图的布局重力设置为右侧。


1
投票

将此代码写入 Main.java,im1 是 xml 文件右上角的导航栏图标。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    im1=findViewById(R.id.humburgericon);
    im1.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("WrongConstant")
        @Override
        public void onClick(View v) {
              drawerLayout.openDrawer(Gravity.END);});

1
投票

可以从右侧进行导航抽屉。 这比看起来容易。 在我看来,最简单的解决方案是:

  1. 扩展 DrawerLayout 类并重写 open() 和 close() 函数,如下所示

     class EndDrawerLayout : DrawerLayout {
         constructor(context: Context) : super(context)
         constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
         constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr)
         override fun open() = openDrawer(GravityCompat.END)
         override fun close() = closeDrawer(GravityCompat.END)
     }
    
  2. 在 XML 中使用自定义 DrawerLayout

  3. 设置NavigationView属性android:layout_gravity="end"

     <com.custom.myapplication.ui.EndDrawerLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true"
         tools:openDrawer="end">
    
     <include
         android:id="@+id/app_bar_main"
         layout="@layout/app_bar_main"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    
     <com.google.android.material.navigation.NavigationView
         android:id="@+id/nav_view"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="end"
         android:fitsSystemWindows="true"
         app:headerLayout="@layout/nav_header_main"
         app:menu="@menu/activity_main_drawer" />
    
     </com.custom.myapplication.ui.EndDrawerLayout>
    
  4. 享受


0
投票

正确答案是: 机器人:layoutDirection =“rtl”

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
tools:openDrawer="start"
tools:context=".MainActivity">
© www.soinside.com 2019 - 2024. All rights reserved.