Android约束布局1.1.3的组可见性不起作用

时间:2019-05-06 21:49:48

标签: android kotlin android-constraintlayout

我只是想将组视图与android约束布局一起使用,以更轻松地设置多个视图的可见性。问题是,即使我将某个组的可见性更改为“消失”或“不可见”,它也无济于事。

我已经在以下线程中尝试过Pavan的响应步骤:toggle visibility of chain group in constraint layout。 但这对我没有任何改变。 (我想是因为我没有使用Beta版)

这是我的依赖项应用程序gradle文件

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.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'
}

这是我与该组的XML文件的摘录

<Button
            android:text="@string/action_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button_update"
            android:layout_marginBottom="32dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent" android:textSize="18sp"/>

<android.support.constraint.Group android:layout_width="wrap_content" android:layout_height="wrap_content"                             android:id="@+id/group"
android:visibility="gone"
app:constraint_referenced_ids="button_update"/>

提前谢谢

2 个答案:

答案 0 :(得分:0)

您从错误的程序包中导入Group小部件。

更改

<android.support.constraint.Group

收件人

<androidx.constraintlayout.widget.Group

更新版本

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
 ..............................................
                                        >

    <Button
        android:id="@+id/button_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="32dp"
        android:text="@string/action_update"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.constraintlayout.widget.Group
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:constraint_referenced_ids="button_update" />

</androidx.constraintlayout.widget.ConstraintLayout>

答案 1 :(得分:0)

问题是,我没有注意到创建我的android studio项目时,没有选中“使用androidx工件”框,该框会自动使您的项目使用新的androidx库。 Androidx倾向于替换支持库(在此处查看更多详细信息:https://developer.android.com/jetpack/androidx

所以基本上,我按照here给出的步骤将项目转换为androidx,现在一切正常。

感谢您的宝贵时间@ theapache64,现在您的代码正在运行。