【问题标题】:ConstraintLayout not showing child viewsConstraintLayout 不显示子视图
【发布时间】:2018-09-06 17:40:58
【问题描述】:

我正在以编程方式隐藏和显示一个文本视图。父约束布局已设置高度“wrap_content”。

但这里的问题是,如果最初隐藏 textview,即使我将 textview 可见性设置为 VISIBLE,ConstraintLayout titleLayout 也会保持折叠状态并且不显示子 textview。

将 ConstraintLayout 的高度设置为 wrap_content 很重要,这样布局可以根据 textview 内容进行增长/缩小。

 <android.support.constraint.ConstraintLayout
            android:paddingTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

 <android.support.constraint.ConstraintLayout
            android:id="@+id/descriLayout"
            android:layout_width="match_parent"
            app:layout_constraintTop_toBottomOf="@id/titleLayout"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="5dp"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/box_description"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:text="TextView"
                android:textSize="12sp"
                android:maxLines="5"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
        </android.support.constraint.ConstraintLayout>
   </android.support.constraint.ConstraintLayout>

【问题讨论】:

  • layout.xml 的完整 xml 代码在这里会更有帮助。
  • Linear Layout 帮了我...我用 LinearLayout 更改了 constraintLayout,并按预期工作。

标签: android android-layout android-constraintlayout


【解决方案1】:

如果要更改 ConstraintLayout 中子项的可见性,则必须通过 ConstraintSet 进行:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.setVisibility(childView.getId(), View.VISIBLE);
constraintSet.applyTo(constraintLayout);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多