ConstraintLayout间歇性布局失败

ConstraintLayout intermittent layout failure

ConstraintLayout 在恢复 activity 后不久将视图从 GONE 设置为 VISIBLE 时,间歇性地无法正确布局:

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/text1"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>



override fun onResume() {
        super.onResume()

        text1.text = ""
        text1.visibility = View.GONE

        text2.text = ""
        text2.visibility = View.GONE

        text1.postDelayed({
                    text1.text = "Hello"
                    text1.visibility = View.VISIBLE

                    text2.text = "World"
                    text2.visibility = View.VISIBLE
                }, 100
        )
    }

Full source code here

检测 TextView class 显示 TextView 实例测量正确,但在布局时它们的宽度设置为 0。

我想知道 ConstraintLayout LinearSystem 是否是不确定的。是否有在未定义迭代顺序的地方迭代的地图? (我在食火鸡身上看过这个)

我正在查看您在 github page 中的声明:

ConstraintLayout intermittently fails to layout correctly when a view is set from GONE to VISIBLE shortly after an activity is resumed

我查看了您的项目并将 100 毫秒更改为 1000 毫秒。 这是输出:

在我看来,您希望执行 textview.setVisibility(View.GONE) 的那一刻您希望视图不可见。这不是 android 的工作方式。您只是将一个事件发布到 MessageQueue,稍后将由 Looper 处理,而这 100 毫秒不足以让人眼看到发生的这些变化。

这是已在 constraint-layout:1.1.0-beta2 https://issuetracker.google.com/issues/65613481

中修复的 ConstraintLayout 中的错误