Best practice to place menu Icons on the left side of the navigation drawer in Persian,Hebrew devices?

huangapple go评论78阅读模式
英文:

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.

Best practice to place menu Icons on the left side of the navigation drawer in Persian,Hebrew devices?

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;resources&gt;

&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.Light.NoActionBar&quot;&gt;
    &lt;!-- Customize your theme here. --&gt;
    &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
    &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;

&lt;/style&gt;

&lt;style name=&quot;ShaliRTL&quot; parent=&quot;AppTheme&quot;&gt;
    &lt;item name=&quot;android:layoutDirection&quot;&gt;rtl&lt;/item&gt;
&lt;/style&gt;

&lt;/resources&gt;

then in android manifest apply this new style (ShaliRTL) to your specific activity:

        &lt;activity
        android:name=&quot;.YourActivity&quot;
        android:label=&quot;@string/title_activity_settings&quot;
        android:theme=&quot;@style/ShaliRTL&quot;
        android:screenOrientation=&quot;fullSensor&quot; /&gt;

good luck.

huangapple
  • 本文由 发表于 2020年8月2日 18:34:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63214969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定