【问题标题】:Do guidelines in ConstraintLayout support RTLConstraintLayout 中的指南是否支持 RTL
【发布时间】:2017-07-19 13:21:28
【问题描述】:

ConstraintLayout 中的指南是否支持 RTL? 我正在创建一个View,其右侧有个人资料图片和用户信息。我希望图片占宽度的 30%,而用户名和详细信息占 70%。这是指南的有效用例吗?我知道其他实现,但我想知道是否可以在这里使用指南。 我面临的问题是,在将设备语言更改为 RTL 语言后,指南仍保持在左侧的位置

screenshot

这里是 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:background="@color/colorPrimary"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        tools:text="Full Name"
        app:layout_constraintTop_toTopOf="@+id/imageView"
        app:layout_constraintStart_toEndOf="@+id/guideline2" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        tools:text="Android Developer"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintStart_toEndOf="@+id/guideline2" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.30" />


</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    ** 更新 ** 关于此问题的bug 现在已被 Google 标记为已修复。 ** 结束更新 **

    Android 团队似乎忽略了这个问题。

    百分比仅从左侧计算。

    所以要支持 RTL,你可以在 values/dimens.xml 中声明一个常量,例如:

    <resources>
         <item name="my_percent" format="float" type="dimen">0.3</item>
    </resources>
    

    并在values-ldrtl/dimens.xml 文件中用其100-mypercent 覆盖此值。

    <resources>
         <item name="my_percent" format="float" type="dimen">0.7</item>
    </resources>
    

    使用它:

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="@dimen/my_percent" />
    
    • 如果您希望为 RTL 重写完整的布局版本,您可以跳过声明尺寸,并使用 layout-ldrtl/ 目录作为 RTL 版本。

    【讨论】:

    • 谢谢,刚刚查了issue tracker,发现已经被举报了:issuetracker.google.com/issues/38101983
    • 谢谢,我也会留意的。
    • ConstraintLayout 1.1.0 beta 3 androidstudio.googleblog.com/2017/10/…987654323@ 正式解决该问题
    • 我测试了 1.1.0 beta 3。使用 RTL 时,垂直线似乎是从右侧计算的。然而,水平指南现在从底部计算,我认为这是错误的。 RTL 中的 20% 仍然是 20%,而不是 80%。
    猜你喜欢
    • 1970-01-01
    • 2018-12-06
    • 2016-07-20
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多