ConstraintLayout 以编程方式设置constraintHeight_default [英] ConstraintLayout setting constraintHeight_default programmatically
问题描述
我有一个包含 ViewStub
的 ConstraintLayout,其中 app:constraintHeight_default
属性设置为 XML 中的 spread
.我需要能够在运行时更新此属性以具有 wrap
的值,但到目前为止没有任何东西对我有用.这是布局:
I have a ConstraintLayout that contains a ViewStub
with the app:constraintHeight_default
property set to spread
in XML. I need to be able to update this property to have a value of wrap
at runtime, but so far nothing has worked for me. Here is the layout:
<?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
膨胀的布局最终有 0width 和 0 height,因此它不会在布局中呈现.
So far, I've tried using a ConstraintSet
to set constraintHeight_default
programmatically, but the layout inflated from the ViewStub
ends up having 0 width and 0 height, so it isn't rendered in the layout.
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
的高度以包裹其高度?
How can I set the height of the ViewStub
to wrap its height programmatically?
推荐答案
尝试更改以下行:
constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
限制默认高度
constraintSet.constrainDefaultHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
请参阅ConstraintSet 文档.
constrainDefaultHeight
constrainDefaultHeight
void constrainDefaultHeight (int viewId,整数高度)
void constrainDefaultHeight (int viewId, int height)
设置高度的计算方式 MATCH_CONSTRAINT_WRAP 或 MATCH_CONSTRAINT_SPREAD.默认为点差.
Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD. Default is spread.
这篇关于ConstraintLayout 以编程方式设置constraintHeight_default的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!