[英]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.