Android-NavigationView de derecha a izquierda

Estoy usando la última versión de Android Studio (1.5) y quiero hacer un menú usando la disposición de cajones, para la posición de su llamada GravityCompat.

Estoy tratando de usar estos componentes y modificarlo, colocando el cajón de derecha a izquierda. Aquí está mi código.

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; } } 

El 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> 

Como puedes ver, GravityCompat no me deja poner derecho en lugar de END o START, y si lo pongo en el XML, se bloquea.

con el siguiente error:

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

En primer lugar reemplazar en DrawerLayout las tools:openDrawer="start" con las tools:openDrawer="end" . Ahora su problema está en el toggle, que abre el cajón izquierdo, y porque usted tiene solamente el cajón derecho él lanza la excepción. Puede agregar su propio botón en la barra de acción (en el lado derecho) para abrir el cajón. Para hacer que cambiar su app_bar.xml como este

 <?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> 

A continuación, cambie su método onCreate de su actividad como ésta

 @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); } 

También puede encontrar la imagen para su cajón aquí o aquí . Espero que esto ayude. 🙂

utilice un drawwerLayout de encargo apenas como esto:

 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); } } 
  • Android: Cajón de navegación sombra vertical
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.