【发布时间】:2018-06-07 18:28:21
【问题描述】:
我有一个 ConstraintLayout,其中包含一个 ViewStub,其中 app:constraintHeight_default 属性在 XML 中设置为 spread。我需要能够在运行时更新此属性以使其具有wrap 的值,但到目前为止对我来说没有任何效果。这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
>
<ViewStub
android:id="@+id/view_stub"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_view"
app:layout_constraintTop_toBottomOf="@+id/top_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="spread"
/>
<com.example.TopView
android:id="@+id/top_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/view_stub"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
/>
<com.example.BottomView
android:id="@+id/bottom_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/fields_stub"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
/>
到目前为止,我已经尝试使用ConstraintSet 以编程方式设置constraintHeight_default,但是从ViewStub 膨胀的布局最终具有0 宽度和0 高度,因此它不会在布局中呈现。
ConstraintLayout constraintLayout = root.findViewById(R.id.constraint_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
constraintSet.applyTo(constraintLayout);
ViewStub viewStub = root.findViewById(R.id.view_stub);
viewStub.setLayoutResource(R.layout.custom_layout);
viewStub.inflate();
如何设置ViewStub 的高度以编程方式包裹其高度?
【问题讨论】:
-
好奇;如果你膨胀然后更新约束高度会发生什么?
-
@stkent 发生同样的事情。
标签: java android android-layout android-constraintlayout