但我需要的是,我需要从右侧打开相同的抽屉菜单。有谁知道如何实现这个目标?
以下是我的xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="right"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
并在代码中
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@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;
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
答案 0 :(得分:2)
在导航视图中尝试此操作
android:layout_gravity="end"
代替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活动文件中添加这些行,它将起作用
@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);
}
}
答案 1 :(得分:1)
这是可能的。您必须在导航抽屉中设置android:layout_gravity="right"
并更改重力:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
这应该有用!
答案 2 :(得分:0)
实现左侧导航抽屉后,更改以下属性。
DrawerLayout中的tools:openDrawer =“ start”
android:layout_gravity =“ end”在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);
}
}
});
答案 3 :(得分:0)
尝试一下,它对我有用
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} else {
drawer.openDrawer(Gravity.RIGHT);
}
return true;
case R.id.action_settings:
// do what you want
return true;
}
return false;
}