代替android:layout_gravity="start"
<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/header"
app:menu="@menu/drawer"
/>
在Java活動文件//添加這些行試試這個在您的導航視圖
android:layout_gravity="end"
,它會工作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawer.isDrawerOpen(Gravity.RIGHT)) {
mDrawer.closeDrawer(Gravity.RIGHT);
} else {
mDrawer.openDrawer(Gravity.RIGHT);
}
return true;
case R.id.action_settings:
return true;
}
最後更新:
我改變了一些行您的示例項目(MainActivity.java),現在它正在工作,使用此代碼。
不要忘記在你的xml文件中寫android:layout_gravity="end"
來代替android:layout_gravity="start"
。
package com.android4dev.navigationview;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
NavigationView navigationView;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
navigationView=(NavigationView)findViewById(R.id.navigation_view);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer);
// Set a Toolbar to replace the ActionBar.
setToolbarAsActionBar();
// Setup drawer view
setupDrawerContent(navigationView);
// Set the menu icon instead of the launcher icon.
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
//ab.setDisplayShowTitleEnabled(false);
Menu menu = navigationView.getMenu();
MenuItem item = menu.findItem(R.id.starred);
selectDrawerItem(item);
}
private void setToolbarAsActionBar() {
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the planet to show based on
// position
switch (menuItem.getItemId()) {
case R.id.inbox:
Toast.makeText(getApplicationContext(), "Inbox Selected", Toast.LENGTH_SHORT).show();
ContentFragment fragment = new ContentFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.commit();
break;
case R.id.starred:
Toast.makeText(getApplicationContext(), "Stared Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.sent_mail:
Toast.makeText(getApplicationContext(), "Send Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.drafts:
Toast.makeText(getApplicationContext(), "Drafts Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.allmail:
Toast.makeText(getApplicationContext(), "All Mail Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.trash:
Toast.makeText(getApplicationContext(), "Trash Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.spam:
Toast.makeText(getApplicationContext(), "Spam Selected", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
break;
}
drawerLayout.closeDrawers();
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer);
}
}
的可能重複[我怎樣才能打開導航抽屜從右側向左(http://stackoverflow.com/questions/17852091/how-can-i-open-navigation-drawer-from-right側的至左) –
檢查此 > http://stackoverflow.com/questions/17156340/android-is-navigation-drawer-from-right-hand-side-possible –
檢查此 > http://stackoverflow.com/questions/17156340/android-is-navigation-drawer-from-right-hand-side-possible –