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

编程入门 行业动态 更新时间:2024-10-28 06:24:45
本文介绍了在Android中将导航中的菜单项对齐到右侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在android应用中制作了导航抽屉,并使所有内容正确显示以从右侧显示,但是只有我在菜单项向右侧对齐时遇到一个问题,但我不能,该怎么做?

I made the navigation drawer at android app and make everything correctly to appear it from the right side but only I face one problem at the alignment of menu item to the right side but I couldn't , how I can do that?

图片显示了带有左侧菜单项的导航

The picture show navigation with menu items in the left side

这是main_activity布局代码

This is the main_activity layout code

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/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文件:

and this is the activity_main_drawer menu items xml file:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="schemas.android/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>

这些项目中的问题需要在右侧而不是左侧显示,因为我的导航是从右向左打开的

The problem in these items I need to display it at right side not at the left side because my navigation opens from right to left

MainActivity代码:

MainActivity code :

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 给您清单.

If you project is using API level 17 or higher you can add android:supportsRtl to you 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 API将被忽略或不起作用,并且与用户的语言环境选择相关的布局方向,您的应用程序将具有相同的行为(您的布局将始终保留-向右).

This attribute declares whether your application is willing to support right-to-left (RTL) layouts. If set to true and targetSdkVersion is set to 17 or higher, various RTL APIs will be activated and used by the system so your app can display RTL layouts. If set to false or if targetSdkVersion is set to 16 or lower, the RTL APIs will be ignored or will have no effect and your app will behave the same regardless of the layout direction associated to the user's Locale choice (your layouts will always be left-to-right).

帖子中的答案表明,存在一种强制使用RTL模式(如果可用)的方法.在 onCreate()方法中,调用 forceRTLIfSupported().

The answer in this post shows that exists a way to force RTL mode if it is available. In onCreate() method call 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); } }

更多推荐

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

本文发布于:2023-11-16 19:32:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单项   中将   中的菜   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!