我有抽屉类,那个节目,抽屉布局和我用于所有活动 并且有一个抽屉xml,其中包含drawerLayout& NavigationDrawer。
通过拖动它可以正常工作但我想从右边打开抽屉,点击toggleButton。 当我点击toggleButton并尝试从右侧打开导航抽屉时。
异常:java.lang.IllegalArgumentException:找不到重力RIGHT的抽屉视图:
drawerLayout.closeDrawer(Gravity.RIGHT);
我在drawerLayout中使用(tools:openDrawer =“right”)。
我想要一直打开抽屉(rtl和ltr语言)
drawer.xml:
<android.support.constraint.ConstraintLayout 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:layoutDirection="rtl"
tools:context=".ActivityLogin">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:openDrawer="right">
<include
layout="@layout/actionbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
android:layoutDirection="rtl"
android:textDirection="rtl"
app:headerLayout="@layout/drawer_logo"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</android.support.constraint.ConstraintLayout>
drawer.java:
package com.example.farzad.charge;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class Drawer extends AppCompatActivity {
public ConstraintLayout fullLayout;
DrawerLayout drawerLayout;
NavigationView navigationView;
@Override
public void setContentView(int layoutResID) {
fullLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.drawer, null);
super.setContentView(fullLayout);
navigationView = (NavigationView) findViewById(R.id.navigation);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
drawerLayout=new DrawerLayout(G.context);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
}
});
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id=item.getItemId();
if (id==R.id.menu_members){
new MyToast ("clicked", Toast.LENGTH_SHORT);
}
return true;
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} else {
super.onBackPressed();
}
}
}
ActivityMain.java:
package com.example.farzad.charge;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class ActivityMain extends Drawer {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}