【问题标题】:Xamarin Listview doesnt follow the rtl directionXamarin Listview 不遵循 rtl 方向
【发布时间】:2018-06-08 14:32:05
【问题描述】:

伙计们。 我正在学习 xamarin 并尝试为 android 制作一个基本的食物应用程序。这个应用程序是在 RTL 方向。现在我的主要内容中有一个 Listview,其中包含自定义布局列表。下面是这个自定义布局。我是 Xamarin Designer 窗口中的 RTL。

这是布局axaml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:textDirection="rtl">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout2">
    <ImageView
        android:src="@drawable/rating4"
        android:id="@+id/imgRating"
        android:layout_width="70dp"
        android:layout_height="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp" />
    <ImageView
        android:src="@drawable/defaultthumbnail"
        android:id="@+id/imgThumbnail"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp" />
    <TextView
        android:text="10,000 تومان"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:id="@+id/foodPrice"
        android:layout_marginLeft="5dp"
        android:textDirection="rtl" />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginRight="20dp">
    <TextView
        android:text="نام غذا"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtFoodName"
        android:textSize="20sp"
        android:textStyle="bold" />
    <TextView
        android:text="توضیحات غذا"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:id="@+id/txtFoodDesc" />
    <TextView
        android:text="تاریخ"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/txtDate"
        android:textSize="8sp"
        android:textAllCaps="false" />
    <TextView
        android:text="آشپز"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtCook"
        android:textSize="8sp"
        android:textAllCaps="false" />
</LinearLayout>

这是我的主要布局,这个列表视图在其中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainContentLayout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main"
    android:layoutDirection="rtl"
    android:textDirection="rtl">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainContentListView"
        android:layoutDirection="rtl"
        android:textDirection="rtl" />
</LinearLayout>

但是当我运行它时,这就是我得到的

你可以在里面的那 2 个线性布局遵循 RTL 的规则,但是是倒置的。

【问题讨论】:

    标签: android xamarin arabic right-to-left farsi


    【解决方案1】:

    我遇到了同样的问题,我通过在应用程序的清单中添加以下内容来解决它:

    <manifest>
        <application>
        android:supportsRtl="true"
        </application>
    </manifest>
    
    • 默认模拟器和预览不适用于“RTL”语言。尝试在真正的 android 设备上测试或尝试 xamarin 实时播放器。

    • 还有一些android设备在android界面语言为英文时强制“ltr”,尝试将android语言更改为“RTL”语言。

    【讨论】:

    • 我会尽快检查的。
    【解决方案2】:

    关于android:supportsRtl="true",可以参考this,默认为false。

    来自您的layout axaml code,它在 VS 的设计中显示为:

    但它在 AS 的设计中显示为:

    它与你运行的结果相同。

    所以,VS 的设计有问题。

    如果我没有看错你的问题,你想得到的是:

    因此,您需要将layout axaml code 更改为以下内容:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutDirection="rtl"
        android:textDirection="rtl">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2">
            <ImageView
                android:src="@drawable/pause"
                android:id="@+id/imgRating"
                android:layout_width="70dp"
                android:layout_height="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp" />
            <ImageView
                android:src="@drawable/dapao"
                android:id="@+id/imgThumbnail"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp" />
            <TextView
                android:text="10,000 تومان"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textStyle="bold"
                android:id="@+id/foodPrice"
                android:layout_marginLeft="5dp"
                android:textDirection="rtl" />
        </LinearLayout>
    <View
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"/>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginRight="20dp">
            <TextView
                android:text="نام غذا"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txtFoodName"
                android:textSize="20sp"
                android:textStyle="bold" />
            <TextView
                android:text="توضیحات غذا"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:id="@+id/txtFoodDesc" />
            <TextView
                android:text="تاریخ"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/txtDate"
                android:textSize="8sp"
                android:textAllCaps="false" />
            <TextView
                android:text="آشپز"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txtCook"
                android:textSize="8sp"
                android:textAllCaps="false" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • 这与我想要得到的完全相反。在您的第一个屏幕截图中,Vs 设计准确地显示了我想要获得的内容(左侧的缩略图,右侧的文本),但在运行时却相反。不,我不想改变我的控件的位置。因为那么它不会在英文版中以正确的方式出现。
    • 如果我的第一个屏幕截图 Vs 设计显示是你想要的,你只需要在你的第一个 LinearLayout 中删除 android:layoutDirection="rtl"
    • 我知道。这不是我想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多