英文:
Best practice to place menu Icons on the left side of the navigation drawer in Persian,Hebrew devices?
问题
我正在尝试创建一个从右侧打开的导航抽屉(我使用LAYOUT_GRAVITY="END"成功实现了这一点)。现在我希望所有菜单项都从右侧开始,它们的图标也在右侧。
为了实现这一点,我在清单中添加了(SUPPORTSRTL="TRUE"),这也解决了问题。但问题在于:
- 如果设备的语言设置为英语,一切都能正常工作。
- 但如果设备的语言设置为波斯语(这是一种RTL语言),那么应用程序中的其他所有内容都会变成从右到左。这意味着我放在右侧的任何东西都会移动到左侧,而设置为左侧的任何东西都会移动到右侧。我甚至尝试使用"start"和"end"代替"right"和"left",但问题没有解决。
为了解决这个问题,我不得不在每个活动的布局根元素上设置(LAYOUTDIRECTION="LTR")。这解决了问题,现在一切都处于我期望的位置,但这似乎不是一个可持续的解决方案,因为我将来可能会在应用程序中添加此代码行。请问这是否是最佳方法?
我发现其他关于相同主题的问题,但没有一个得到合适的答案。
英文:
I am trying to have a navigation drawer that opens from the right. ( i managed to do that using LAYOUT_GRAVITY="END").
Now I want all the menu items to start from the right and also their Icons on the right too.
To do that I added (SUPPORTSRTL="TRUE") to the manifest and that also solved the problem. But here is the problem:
- if the device's language is set to English, everything works perfectly.
- But if the device's language is set to Persian(which is a RTL language) then everything else in the app will be right to left. That means what ever I put in the right goes to left and what ever that is set to the left goes to the right. I even tried using "start" and "end" instead of "right" and "left" but that didn't solve the problem.
To solve that I had to set the (LAYOUTDIRECTION="LTR") to the root element of layouts on every one of my actitives. That solved the problem and now everything is in the position I desired, however this doesn't seem like a sustainable solution to add this line of code to root element of each activity I will ever add to my app.
Can you please tell me if this is the best way to do it or not?
I found other questions with the same topic with no accepted proper answers.
答案1
得分: 0
这行代码应该解决了您的问题
ViewCompat.setLayoutDirection(drawer, ViewCompat.LAYOUT_DIRECTION_RTL);
drawer
是对您的 DrawerLayout
的引用。
另一种强制将活动布局设置为从右到左(RTL)的方法是使用样式 XML。这是我会这样做的方式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- 自定义您的主题内容。 -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="ShaliRTL" parent="AppTheme">
<item name="android:layoutDirection">rtl</item>
</style>
</resources>
然后,在 Android 清单文件中将这个新样式(ShaliRTL)应用于您的特定活动:
<activity
android:name=".YourActivity"
android:label="@string/title_activity_settings"
android:theme="@style/ShaliRTL"
android:screenOrientation="fullSensor" />
祝您好运。
英文:
this line of code should solve your problem
ViewCompat.setLayoutDirection(drawer, ViewCompat.LAYOUT_DIRECTION_RTL);
drawer is a reference to your DrawerLayout.
an other way to force RTL layout to activities is using styles xml. this is how i whould do it:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="ShaliRTL" parent="AppTheme">
<item name="android:layoutDirection">rtl</item>
</style>
</resources>
then in android manifest apply this new style (ShaliRTL) to your specific activity:
<activity
android:name=".YourActivity"
android:label="@string/title_activity_settings"
android:theme="@style/ShaliRTL"
android:screenOrientation="fullSensor" />
good luck.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论