【问题标题】:Group visibility with android constraint layout 1.1.3 not workingandroid约束布局1.1.3的组可见性不起作用
【发布时间】:2019-09-24 12:58:48
【问题描述】:

我只是尝试使用带有 android 约束布局的组视图来更轻松地设置多个视图的可见性。问题是即使我将组的可见性更改为消失或不可见,它也不会做任何事情。

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

这是我的依赖应用程序 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"/>

【问题讨论】:

    标签: android kotlin android-constraintlayout


    【解决方案1】:

    您正在从错误的包中导入 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>
    

    【讨论】:

    • 非常感谢您的宝贵时间!但是,即使我在我的 gradle 应用程序文件中实现了 android 约束布局,约束布局在预览屏幕中仍显示为灰色。此外,似乎建议使用android文档中所示的支持约束布局:developer.android.com/reference/android/support/constraint/…
    • 不,它根本不起作用,似乎 android studio 无法识别此约束布局,因为我的活动在预览中变灰。在 gradle 中实现后,我尝试重新加载我的项目几次,它没有改变。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多