如何在android studio 1.4的默认情况下从右侧制作导航菜单
Posted
技术标签:
【中文标题】如何在android studio 1.4的默认情况下从右侧制作导航菜单【英文标题】:How to make navigation menu from right side in default of android studio 1.4 【发布时间】:2016-02-12 08:13:06 【问题描述】:我使用 android studio 1.4 的默认导航活动。
然后默认有导航左侧,我想从
制作导航菜单右侧并从左到右更改切换按钮。如何制作
我尝试从头到尾改变navigationview的layout_gravity并改变
GravityCompat.START 到 MainActivity.java 中的 GravityCompat.END,然后报错
当我按下切换按钮时,记录 cat show
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
我的代码(默认来自 android studio 1.4)
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
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.START))
drawer.closeDrawer(GravityCompat.START);
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
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_camara)
// 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)
else if (id == R.id.nav_share)
else if (id == R.id.nav_send)
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<include layout="@layout/app_bar_main" android:layout_
android:layout_ />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_ android:layout_
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />
谢谢你 对不起我的英语不好。
【问题讨论】:
您是否尝试过正确设置重力?像这样:android:layout_gravity="right"
。然后在您的代码中:if (drawer.isDrawerOpen(GravityCompat.RIGHT)) drawer.closeDrawer(GravityCompat.RIGHT);
我试试这个,然后它显示错误“java.lang.IllegalArgumentException:没有找到重力LEFT的抽屉视图”
【参考方案1】:
用Gravity.RIGHT
代替GravityCompat.START
【讨论】:
并在 xml 中使用 android:layout_gravity="rifgt" 而不是 "start"。 当我按下toggleButton java.lang.IllegalArgumentException 时不能和logcat 显示错误:没有找到重力LEFT 的抽屉视图【参考方案2】:public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
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 boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (item != null && item.getItemId() == android.R.id.home)
if (drawer.isDrawerOpen(Gravity.LEFT))
drawer.closeDrawer(Gravity.LEFT);
else
drawer.openDrawer(Gravity.LEFT);
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_camara)
// 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)
else if (id == R.id.nav_share)
else if (id == R.id.nav_send)
drawer.closeDrawer(Gravity.RIGHT);
return true;
@Override
public void onStart()
super.onStart();
if (drawer_layout.isDrawerOpen(Gravity.RIGHT))
drawer_layout.closeDrawer(Gravity.RIGHT);
【讨论】:
以上是关于如何在android studio 1.4的默认情况下从右侧制作导航菜单的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio3.1.4如何添加Genymotion插件并显示
android studio 1.4中content_main.xml的作用是啥?
在新的 Navigation Drawer Activity 模板中使用 onNavigationItemSelected 在 Fragments 之间切换(Android Studio 1.4 及更
如何在所有活动中使用Android Studio默认导航抽屉[重复]