Android导航抽屉切换图标到右边

时间:2016-08-24 20:20:47

标签: android navigation-drawer

我的要求是下面的图片我的导航抽屉应该从右侧打开。我实现了这个。我的导航抽屉从右到左打开。但问题是切换图标始终在左侧。如何将切换图标设置为右侧。我检查了以下SO问题,但没有一个得到任何帮助。

Change toggle button image Icon In Navigation Drawer right to left

Drawer Toggle in right Drawer

enter link description here

enter image description here

这是我尝试过的。

我的布局代码 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);
                }
            }
        });
    }

}

提前致谢。

2 个答案:

答案 0 :(得分:37)

我为EndDrawerToggle类创建了一个非常类似于您的设置 - DrawerLayout带有一个末端对齐的抽屉View,位于AppCompatActivity中,带有自定义{{ 1}}作为支持Toolbar

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,然后同步即可。我建议在DrawerListener方法中同步切换,以确保它正确同步,例如,在方向更改后。

onPostCreate()

答案 1 :(得分:0)

在你的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,则必须创建自定义菜单并覆盖OnCreateOptions方法(除非我有另外一种方式#&# 39;我不知道,这绝对是可能的。)

https://developer.android.com/guide/topics/manifest/application-element.html#supportsrtl