在 ConstraintLayout 中,当我将视图的 layoutDirection 设置为 "rtl" 时,出现了一些问题。如何避免这种情况?

In ConstraintLayout when I set a layoutDirection of a view to "rtl" some thing go wrong. How to avoid this?

我的示例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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/toBeUpdated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layoutDirection="rtl"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="@string/update_text_view"
        android:onClick="updateTextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="274dp" />

</android.support.constraint.ConstraintLayout>

我希望我的 (toBeUpdated) textView 具有 "rtl" layoutDirection。使用这段代码,我们上面的一切都很好,直到必须更新 textView 为止。在(出于任何原因)我在 运行 时间更新此 textView 之后,视图跳到指南的左侧。我发现出了什么问题。这种突然跳跃背后的原因是,当方向从右到左时,right 是起点,left 是视野的终点(如你所知与往常完全相反)。所以我这个 xml 文件的新版本和更正版本是解决这个问题的 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/toBeUpdated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layoutDirection="rtl"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="@string/update_text_view"
        android:onClick="updateTextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="274dp" />

</android.support.constraint.ConstraintLayout>

在这个版本中,起初,textView 位置不对,但更新后,它跳转到我想要的位置。与第一种情况完全相反,其中 textView 位于正确的位置,直到它没有更新,然后发生跳转。 在这两种情况下,都会发生突然的不需要的跳跃。它可能与其他视图重叠或被它们重叠。 最重要的是,也许我不希望它更新一次,因为它是一个静态标题。我该怎么办? 这是 Bug 还是什么?

build.gradle(模块:应用程序)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "example.com.testconstraintlayout"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

我想不出有什么理由在更新内容时文本视图应该跳转。我认为这是一个错误,它可能与 left/right 和 start/end 不时出现的混淆有关。

您没有显示屏幕截图,我无法确定您想要实现的目标,但我认为您的解决方法是使用 left/right 而不是 start/end 为文本视图如下:

<TextView
    android:id="@+id/toBeUpdated"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layoutDirection="rtl"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent" />

我已经试过了,视图不会像 start/end 那样跳转。