gpt4 book ai didi

android - 约束布局 : set height of all views in row to match the tallest one

转载 作者:IT老高 更新时间:2023-10-28 23:25:20 26 4
gpt4 key购买 nike

我正在尝试使用 ConstraintLayout(1.0.2 版)来设置 2 个并排 View 的高度以匹配其中最高的一个。这用作 RecyclerView 中的 ViewHolder,其中每个 TextView 获取任意长度的文本...

如果我将每个设置为 wrap_content,则较短的会缩小。如果我将两者都设置为 0dp(match_contraints),则两者最终都为 0 高度。

设置如下:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/id1"
android:layout_width="60dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/id2"/>

<TextView
android:id="@+id/id2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/id1"
app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

我想这是一个错误,因为“0dp”应该更像 match_parent 而不是实际的 0 dp。

顺便说一下,在 iOS 上,等效的自动布局规则(设置 View 的高度以匹配父级的顶部和底部)会产生预期的结果。

当然,使用LinearLayout这很简单,但是这个布局在更大的布局中起作用,我想修剪 View 层次......

最佳答案

正在回答,以防将来有人在寻找答案。

ConstraintLayout介绍Barrierv1.1 中实现 OP 在问题中提出的一项此类功能

上面提到的功能可以使用下面的xml来实现:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/id1"
android:layout_width="60dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/barrier"
app:layout_constraintVertical_bias="0.0"
android:text="@string/id1_text" />

<TextView
android:id="@+id/id2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/id1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/barrier"
app:layout_constraintVertical_bias="0.0"
android:text="@string/id2_text" />

<android.support.constraint.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="id1,id2" />

</android.support.constraint.ConstraintLayout>

关于android - 约束布局 : set height of all views in row to match the tallest one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43288173/

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