简体   繁体   English

Android使用NavigationView从右向左导航抽屉

[英]Android implement right to left navigation drawer with NavigationView

How can i open navigation drawer from right from 10+ api? 如何从10+ API正确地打开导航抽屉?

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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: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="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        android:touchscreenBlocksFocus="false"/>

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

And

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="@dimen/nav_header_height"
              android:background="@drawable/side_nav_bar"
              android:gravity="bottom"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com"/>

</LinearLayout>

Unfortunately i could not found a solution in order to open navigation drawer from right, when i using layout_gravity="right" and tools:openDrawer="right" , i getting No drawer view found with gravity LEFT error, i think there is not any official way, should i still use Recycle view instead of NavigationView ? 不幸的是,当我使用layout_gravity="right"tools:openDrawer="right"No drawer view found with gravity LEFT从右打开导航抽屉的解决方案,我No drawer view found with gravity LEFT错误的No drawer view found with gravity LEFT ,我认为没有任何No drawer view found with gravity LEFT官方方式,我还是应该使用Recycle view而不是NavigationView吗?

In the Activity's onOptionsItemSelected() method, instead of calling the ActionBarDrawerToggletoggle#onOptionsItemSelected() method, we check if the MenuItem 's ID is equal to the Home Button 's ID, and toggle the drawer state ourselves. 在Activity的onOptionsItemSelected()方法中,我们将检查MenuItem的ID是否等于Home Button的ID,然后自己切换抽屉状态,而不是调用ActionBarDrawerToggletoggle#onOptionsItemSelected()方法。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home) {
        if(drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
            drawerLayout.closeDrawer(Gravity.RIGHT);
        }
        else {
            drawerLayout.openDrawer(Gravity.RIGHT);
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Whichever View you use for the drawer doesn't really matter. 无论您使用哪个抽屉View都没有关系。

I found the solution here.Its working exactly what you want. 我在这里找到了解决方案,它完全可以按照您想要的方式工作。

https://stackoverflow.com/a/34097553/4156299 https://stackoverflow.com/a/34097553/4156299

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

相关问题 左右导航抽屉 - Left and Right Navigation Drawer Android 导航抽屉:在 NavigationView 中垂直居中菜单 - Android navigation drawer: center menu vertically in NavigationView 使用android设计支持库的从右到左导航抽屉菜单 - Right to left navigation drawer menu using android design support library 无法在屏幕右侧实现导航抽屉-Android / Java - Unable to Implement a Navigation Drawer on the Right Side of The Screen - Android / Java Android导航组件+BottomNavigationView+NavigationView(导航抽屉)使用方法 - How to use Android Navigation Component + BottomNavigationView+ NavigationView (Navigation Drawer) Android导航抽屉左边缘 - Android Navigation Drawer left margin 在NavigationView中从右到左的菜单项Android - Right to left menu items Android in NavigationView 如何在Android Eclipse的“导航抽屉活动模板”中使“图标”显示在“导航抽屉”的右侧和“ mTitle”的左侧? - How to make Icon appear to the right of Nav Drawer and left of mTitle in Navigation Drawer Activity Template in Android Eclipse? Android-NavigationView从右到左 - Android-NavigationView from right to left 如何从右到左设置工具栏导航抽屉? - How to set toolbar navigation drawer right to left?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM