将导航中的菜单项对齐到 android 中的右侧

Align menu item at Navigation to the right side in android

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

}

如果您的项目使用 API 17 级或更高级别,您可以将 android:supportsRtl 添加到 manifest.

<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 APIs 将被忽略或无效,并且无论与用户的区域设置相关联的布局方向(您的布局将始终从左到右)。

编辑:post 中的答案表明存在一种强制 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);
    }
}
<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"/>