Android-NavigationView 从右到左

Android-NavigationView from right to left

我正在使用 Android Studio (1.5) 的最新版本,我想使用抽屉布局制作一个菜单,用于定位其调用 GravityCompat。

我正在尝试使用此组件并对其进行修改,将抽屉从右向左放置。这是我的代码。

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.END)) {
        drawer.closeDrawer(GravityCompat.END);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.END);
    return true;
}
}

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_layout"
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="wrap_content"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />

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

如您所见,GravityCompat 不允许我将 END 或 START 放在右边,如果我将它放在 XML 中,它会崩溃。

下一个错误:

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

首先将 DrawerLayout 上的 tools:openDrawer="start" 替换为 tools:openDrawer="end"。现在你的问题出在打开左边抽屉的开关上,因为你只有右边的抽屉,它会抛出异常。您可以在操作栏(右侧)上添加自己的按钮以打开抽屉。为此,请像这样 app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

                <FrameLayout
                    android:id="@+id/drawer_button"
                    android:layout_width="50dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_alignParentRight="true"
                    android:clickable="true">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:src="@mipmap/ic_drawer" />
                </FrameLayout>


        </RelativeLayout>


    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

然后像这样Activity改变你的onCreate方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    findViewById(R.id.drawer_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // open right drawer
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.openDrawer(GravityCompat.END);
        }
    });

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

您还可以找到抽屉的图像 here or here。 我希望这能帮到您。 :)

像这样使用自定义 drawerLayout :

public class TaskManagerDrawerLayout  extends DrawerLayout {

public TaskManagerDrawerLayout(Context context) {
    super(context);
}

public TaskManagerDrawerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TaskManagerDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return false;
    }
}

@Override
public void closeDrawer(int gravity) {
    super.closeDrawer(GravityCompat.END);
}

@Override
public void openDrawer(int gravity) {
    super.openDrawer(GravityCompat.END);
}

}

实施左侧导航抽屉后更改以下属性。

tools:openDrawer="start" in DrawerLayout
android:layout_gravity="end" in NavigationView  
toolbar.setNavigationIcon (R.color.transparent); mDrawerToggle.setDrawerIndicatorEnabled (false);

toolbar.setNavigationOnClickListener (new View.OnClickListener () { @Override public void onClick(View view) { if (mDrawerLayout.isDrawerOpen (Gravity.END)) { mDrawerLayout.closeDrawer (Gravity.END); } else { mDrawerLayout.openDrawer (Gravity.END); } } });