我在Android应用程序中创建了导航抽屉,并使所有内容正确显示在右侧,但只有我在菜单项对齐右边时遇到一个问题,但我无法做到,我怎么做是什么?
这是main_activity布局代码
<?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="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
这是activity_main_drawer菜单项xml文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single" >
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Camera" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
这些项目中的问题我需要在右侧显示而不是在左侧,因为我的导航从右向左打开
MainActivity代码:
public class ParentMainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_parent);
/*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, "You have chosen mail option", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ImageButton menuRight = (ImageButton) findViewById(R.id.menuRight);
menuRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
drawer.openDrawer(GravityCompat.END);
}
}
});
/*ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();*/
NavigationView navigationView1 = (NavigationView) findViewById(R.id.nav_view);
navigationView1.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();
String text = "";
if (id == R.id.nav_camera) {
text = "camera";
} else if (id == R.id.nav_gallery) {
text = "gallery";
} else if (id == R.id.nav_slideshow) {
text = "slideshow";
} else if (id == R.id.nav_manage) {
text = "tools";
} else if (id == R.id.nav_share) {
text = "share";
} else if (id == R.id.nav_send) {
text = "send";
Toast.makeText(this, "You have chosen " + text, Toast.LENGTH_LONG).show();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.END);
return true;
}
}
答案 0 :(得分:0)
如果您的项目使用的是API级别17或更高级别,则可以将<{3}}添加到清单。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
</application>
此属性声明您的应用程序是否愿意支持从右向左(RTL)布局。如果设置为 true 且targetSdkVersion设置为17或更高,则系统将激活并使用各种RTL API,以便您的应用可以显示RTL布局。如果设置为false或者targetSdkVersion设置为16或更低,则RTL API将被忽略或无效,并且无论与用户的Locale选项关联的布局方向如何,您的应用都将表现相同(您的布局将始终保持不变-to-右)。
修改强> 这个android:supportsRtl中的答案表明,如果RTL模式可用,则存在一种强制RTL模式的方法。在 onCreate()方法中调用 forceRTLIfSupported()。
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
答案 1 :(得分:0)
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
android:layoutDirection="rtl" // set layoutDirection to 'rtl
app:itemIconTint="@drawable/drawer_selectcolor_imagecolor"
app:itemTextColor="@drawable/drawer_selectcolor_textcolor"
app:menu="@menu/nav_menu"/>