gpt4 book ai didi

java - ConstraintLayout 以编程方式设置constrainHeight_default

转载 作者:行者123 更新时间:2023-12-02 11:08:36 33 4
gpt4 key购买 nike

我有一个 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 的高度以通过编程方式包裹其高度?

最佳答案

尝试更改以下行:

constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);

约束默认高度

constraintSet.constrainDefaultHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);

请参阅ConstraintSet documentation .

constrainDefaultHeight

void constrainDefaultHeight (int viewId, int height)

Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD. Default is spread.

关于java - ConstraintLayout 以编程方式设置constrainHeight_default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747930/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com