Android导航抽屉切换图标向右 [英] Android navigation drawer toggle icon to right
问题描述
我的要求如下图所示.我的导航抽屉应从右侧打开.我已经实现了.我的导航抽屉从右到左打开.但是问题是切换图标始终在左侧.如何在右侧设置切换图标?
My requirement is shown in the picture below My navigation drawer should be opened from the right side. I have implemented this. My navigation drawer open from right to left. But the problem is toggle icon is always on the left side. How can I set toggle icon to the right?
我已经检查了以下SO问题,但没有一个得到任何帮助:
I have checked the following SO questions, but none of them came to any help:
这是我尝试过的:
我的布局的代码 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
>,并使用自定义Toolbar
作为支持ActionBar
.
I wrote the EndDrawerToggle
class for a setup very similar to yours - a DrawerLayout
with an end-aligned drawer View
, in an AppCompatActivity
with a custom Toolbar
as the support 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
方法仍可用于覆盖,但对于基本功能而言则不必这样做,因为EndDrawerToggle
自行处理切换抽屉状态.同样,也无需自己处理切换点击,因此也不需要导航OnClickListener
.
The EndDrawerToggle
class is a complete replacement for ActionBarDrawerToggle
in this case, so you will not need any of the setup that you currently have for that. All of the DrawerListener
methods are still available for override, but it is not necessary to do so for the basic functionality, as EndDrawerToggle
handles toggling the drawer state on its own. It is similarly unnecessary to handle the toggle click yourself, so you do not need the navigation OnClickListener
either.
只需实例化切换开关,将其添加为DrawerListener
,然后对其进行同步.我建议在onPostCreate()
方法中同步切换,以确保它正确同步,例如在方向更改后.
Simply instantiate the toggle, add it as a DrawerListener
, and sync it. I would recommend syncing the toggle in the onPostCreate()
method, to ensure that it syncs correctly, for example, after an orientation change.
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导航抽屉切换图标向右的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!