【问题标题】:ConstraintLayout intermittent layout failureConstraintLayout 间歇性布局失败
【发布时间】:2017-09-12 15:17:20
【问题描述】:

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

<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 类显示TextView 实例被正确测量,但在布局时它们的宽度设置为0。

我想知道 ConstraintLayout LinearSystem 是否是不确定的。是否存在迭代未定义迭代顺序的映射? (我在 Cassowary 上见过这个)

【问题讨论】:

  • 您是否能够在不使用 kotlin 的项目中重现这一点?我做不到,但它可能是特定于设备的。
  • 不,我没有尝试过没有 Kotlin。如果它与 Kotlin 语言有关,我会感到非常惊讶。你在这里尝试过这个项目吗? github.com/alexbirkett/ConstraintLayoutBug 在较慢的旧手机上重现似乎最容易。我在视频中使用了三星 Galaxy S3
  • 我下载了它,但我没有安装 Android Studio 3 测试版。当然,我可以安装它或尝试让项目使用不同的 gradle 插件,但这足以让我不想打扰。另外,如果我更改了插件,然后我无法重现它,那真的有用吗?

标签: android android-layout kotlin android-view android-constraintlayout


【解决方案1】:

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

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

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

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

【讨论】:

  • 100 毫秒后视图被设置为可见,而不是不可见,但有时它们不会出现。我没有尝试使用模拟器进行复制,但它发生在速度较慢的设备上,例如三星盖乐世 S3
  • The views are being set to visible, not invisible 我说的是这条线text1.visibility = View.GONEit happens on slower devices 表示 100 毫秒是不够的。
  • 即使布局异步发生,也应该可以将可见性从 GONE 设置为 VISIBLE 而无需等待任意时间
  • 只是为了仔细检查我使用 RelativeLayout 而不是 ConstraintLayout 进行测试,但我无法重现该错误。
【解决方案2】:

这是在 constraint-layout:1.1.0-beta2 https://issuetracker.google.com/issues/65613481 中修复的 ConstraintLayout 中的错误

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多