Android导航抽屉切换图标向右
我的要求是下面的图片我的导航抽屉应该从右侧打开。 我已经实现了这一点。 我的导航抽屉从右向左打开。 但问题是切换图标总是在左侧。 我怎样才能设置切换图标的权利。 我检查了以下的问题,但没有一个人得到任何帮助。
更改切换button图像图标在导航抽屉从右到左
抽屉在右侧抽屉中切换
在这里input链接描述
这是我所尝试过的。
代码为我的布局activity_main.xml
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="end"> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.example.nav.MainActivity" android:foregroundGravity="right"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="end" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:layout_gravity="right" app:popupTheme="@style/AppTheme.PopupOverlay" android:foregroundGravity="right" android:textAlignment="viewEnd" android:touchscreenBlocksFocus="false" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_gravity="end" app:headerLayout="@layout/nav_header" app:menu="@menu/menu_navigation" android:textAlignment="viewEnd" /> </android.support.v4.widget.DrawerLayout>
代码为我的活动
public class MainActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); initNavigationDrawer(); } @TargetApi(Build.VERSION_CODES.M) public void initNavigationDrawer() { NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { int id = menuItem.getItemId(); switch (id){ case R.id.home: Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_SHORT).show(); drawerLayout.closeDrawers(); break; case R.id.settings: Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_SHORT).show(); break; case R.id.trash: Toast.makeText(getApplicationContext(),"Trash",Toast.LENGTH_SHORT).show(); drawerLayout.closeDrawers(); break; case R.id.logout: finish(); } return true; } }); drawerLayout = (DrawerLayout)findViewById(R.id.drawer); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){ @Override public void onDrawerClosed(View v){ super.onDrawerClosed(v); } @Override public void onDrawerOpened(View v) { super.onDrawerOpened(v); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item != null && item.getItemId() == android.R.id.home) { if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) { drawerLayout.closeDrawer(Gravity.RIGHT); } else { drawerLayout.openDrawer(Gravity.RIGHT); } } return false; } }; drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) { drawerLayout.closeDrawer(Gravity.RIGHT); } else { drawerLayout.openDrawer(Gravity.RIGHT); } } }); } }
提前致谢。
我为EndDrawerToggle
类编写了一个非常类似于您的设置 – 一个DrawerLayout
和一个结束alignment的抽屉View
,在一个具有自定义Toolbar
的AppCompatActivity
作为支持ActionBar
。
import android.app.Activity; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.graphics.drawable.DrawerArrowDrawable; import android.support.v7.widget.AppCompatImageButton; import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar.LayoutParams; import android.view.View; import android.view.View.OnClickListener; public class EndDrawerToggle implements DrawerLayout.DrawerListener { private DrawerLayout drawerLayout; private DrawerArrowDrawable arrowDrawable; private AppCompatImageButton toggleButton; private String openDrawerContentDesc; private String closeDrawerContentDesc; public EndDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) { this.drawerLayout = drawerLayout; this.openDrawerContentDesc = activity.getString(openDrawerContentDescRes); this.closeDrawerContentDesc = activity.getString(closeDrawerContentDescRes); arrowDrawable = new DrawerArrowDrawable(toolbar.getContext()); arrowDrawable.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_END); toggleButton = new AppCompatImageButton(toolbar.getContext(), null, R.attr.toolbarNavigationButtonStyle); toolbar.addView(toggleButton, new LayoutParams(GravityCompat.END)); toggleButton.setImageDrawable(arrowDrawable); toggleButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { toggle(); } } ); } public void syncState() { if (drawerLayout.isDrawerOpen(GravityCompat.END)) { setPosition(1f); } else { setPosition(0f); } } public void toggle() { if (drawerLayout.isDrawerOpen(GravityCompat.END)) { drawerLayout.closeDrawer(GravityCompat.END); } else { drawerLayout.openDrawer(GravityCompat.END); } } public void setPosition(float position) { if (position == 1f) { arrowDrawable.setVerticalMirror(true); toggleButton.setContentDescription(closeDrawerContentDesc); } else if (position == 0f) { arrowDrawable.setVerticalMirror(false); toggleButton.setContentDescription(openDrawerContentDesc); } arrowDrawable.setProgress(position); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { setPosition(Math.min(1f, Math.max(0, slideOffset))); } @Override public void onDrawerOpened(View drawerView) { setPosition(1f); } @Override public void onDrawerClosed(View drawerView) { setPosition(0f); } @Override public void onDrawerStateChanged(int newState) { } }
在这种情况下, EndDrawerToggle
类是ActionBarDrawerToggle
的完全替代品,因此您不需要任何您现在拥有的设置。 所有DrawerListener
方法仍然可用于重写,但是对于基本function来说, EndDrawerToggle
,因为EndDrawerToggle
处理自己切换抽屉状态。 同样不需要自己处理切换点击,所以你也不需要导航OnClickListener
。
只需实例化切换,将其添加为DrawerListener
,并将其同步。 我build议同步onPostCreate()
方法中的切换,以确保它在同步方向更改后正确同步。
private EndDrawerToggle drawerToggle; ... public void initNavigationDrawer() { NavigationView navigationView = ... ... drawerLayout = (DrawerLayout)findViewById(R.id.drawer); drawerToggle = new EndDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close); drawerLayout.addDrawerListener(drawerToggle); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState(); }
在你的Android清单中添加这一行:
android:supportsRtl="true"
到你的应用程序,像这样:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true"
然后在你的onCreate方法中,添加这一行:
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
警告:::这只适用于SdkVersion 17+,所以如果你的应用程序的目标是一个较低的最低SDK,你将不得不创build一个自定义菜单并重写OnCreateOptions方法(除非有另一种我不知道的方式,这是绝对可能)。
https://developer.android.com/guide/topics/manifest/application-element.html#supportsrtl