android - MenuItem 图标在 RTL 布局中未正确显示

标签 android android-layout right-to-left android-menu optionmenu

在 RTL 中,选项菜单项图标显示不正确!! 但在 LTR 中,一切都显示得很好很漂亮。

在这个命令的帮助下,我制作了RTL程序

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

我的菜单layout.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="start"
    android:layoutDirection="rtl"
    android:layout_gravity="start">
    <item
        android:id="@+id/action_more"
        android:icon="@drawable/ic_add_white_24dp"
        android:title=""
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/action_settings"
                android:icon="@drawable/ic_wb_sunny_black_24dp"
                android:title="آیتم شماره 1"/>

            <item
                android:id="@+id/action_settings2"
                android:icon="@drawable/ic_star_black_24dp"
                android:title="آیتم شماره 2"/>

            <item
                android:id="@+id/action_settings3"
                android:icon="@drawable/ic_wb_sunny_black_24dp"
                android:title="آیتم شماره 3"/>
        </menu>
    </item>
</menu>

请帮我解决这个问题。

enter image description here Screenshot

最佳答案

我在 RTL 布局中遇到了同样的问题,如果您使用的是 Support LibraryAndroidX,您可以应用这个简单的修复:

  1. 在您的项目中,使用此名称 abc_list_menu_item_icon.xmlres -> layout 中创建新的布局文件。
  2. 将下面的代码复制到步骤 1 中的文件中。

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2007 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
    
              http://www.apache.org/licenses/LICENSE-2.0
    
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/icon"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"
               android:layout_marginLeft="8dip"
               android:layout_marginRight="-8dip"
               android:layout_marginStart="8dip"
               android:layout_marginEnd="-8dip"
               android:layout_marginTop="8dip"
               android:layout_marginBottom="8dip"
               android:scaleType="centerInside"
               android:duplicateParentState="true"/>
    

关于android - MenuItem 图标在 RTL 布局中未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54179916/

相关文章:

android - 是否有其他解决方案而不是嵌套的 Recyclerview

java - 将操作栏添加到自定义对话框

android - 如何将textview对齐到屏幕的右上角

javascript - HTML5 模板中未显示希伯来语字符

Android Center 布局隐藏按钮

Android 相机 Intent ACTION_IMAGE_CAPTURE 导致图像 90 度方向

iphone - 获取 "uitextview"iphone的书写语言?

html - "End"- 对齐 CSS 中的元素

android - "shake to do something"代码解释

android - 如何在 Android 的布局 XML 文件中设置 View 的属性?