android - 无法设置约束组的可见性

标签 android kotlin android-constraintlayout kotlin-android-extensions

当我尝试在按钮单击时设置组的可见性时,它不会影响 View 的可见性。使用 com.android.support.constraint:constraint-layout:1.1.0-beta4。我尝试按元素设置它没有问题,但组没有成功。

我的 MainActivity.kt

private fun toggleLoginUI(show: Boolean) {
    if (show) {
        group.visibility = VISIBLE
    } else {
        group.visibility = INVISIBLE
    }
}

fun onClick(view: View) {
    when (view.id) {
        R.id.button -> toggleLoginUI(true)
        R.id.button4 -> toggleLoginUI(false)
    }
}

我的activity_main.xml

    <android.support.constraint.ConstraintLayout..

            <TextView
                android:id="@+id/textView"
... />

            <TextView
                android:id="@+id/textView2"
... />

            <Button
                android:id="@+id/button"
.../>

            <Button
                android:id="@+id/button4"
... />


            <android.support.constraint.Group
                android:id="@+id/group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible"
                app:constraint_referenced_ids="textView,textView2" />
            </android.support.constraint.ConstraintLayout>

最佳答案

更新:这已在 ConstraintLayout 版本 2.0.0 beta 6 中报告为已修复。请参阅 bug fixes for ConstraintLayout 2.0.0 beta 6 .



这对我来说似乎是一个错误。 GONE 有效,但 INVISIBLE 无效,我认为应该如此。除非有人可以在我的想法错误的地方发布错误报告,否则它可能值得报告。 (我正在使用 constraint-layout:1.1.0-beta4。)

同时,这里有一个解决方法,它显式检索组内的 id 并设置每个检索到的 View 的可见性。

在 MainActivity.kt 中

private fun toggleLoginUI(show: Boolean) {
    if (show) {
        setGroupVisibility(mLayout, group, Group.VISIBLE)
    } else {
        setGroupVisibility(mLayout, group, Group.INVISIBLE)
    }
}

private fun setGroupVisibility(layout: ConstraintLayout, group: Group, visibility: Int) {
    val refIds = group.referencedIds
    for (id in refIds) {
        layout.findViewById<View>(id).visibility = visibility
    }
}

mLayoutConstraintLayout

更新:这是另一个解决方法,它利用了 GONE 的更改按预期工作这一事实:

private fun toggleLoginUI(show: Boolean) {
    if (show) {
        group.visibility = GONE
        group.visibility = VISIBLE
    } else {
        group.visibility = GONE
        group.visibility = INVISIBLE
    }
}

关于android - 无法设置约束组的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47865436/

相关文章:

android - 没有 jar 文件的外部库

java - 应用程序在几个小时后停止刷新?

Android - 我的简单 ListView/Toast 应用程序在启动时崩溃......有什么想法吗?

java - 在 4.2.2 及更高版本的操作系统中获取 java.lang.NoSuchMethodError : android. os.FileUtils.getFatVolumeId

kotlin - kotlin:列出任何对象的属性-方差错误

android - 没有办法编译我自己的 kotlin 库

Android - 如何为约束布局参数设置动画?

安卓 : How to programmatically set layout_constraintRight_toRightOf "parent"

javafx-2 - Kotlin 中的 TableView 回调

android - ConstraintSet.createHorizo​​ntalChain 非法参数异常 : must have 2 or more widgets in a chain