ConstraintLayout:设置行中所有视图的高度以匹配最高的视图 [英] ConstraintLayout: set height of all views in row to match the tallest one
问题描述
我正在尝试使用ConstraintLayout(版本1.0.2)来设置2个并排视图的高度,以匹配其中一个最高的视图.用作RecyclerView的ViewHolder,其中每个TextView都可以获取任意长度的文本...
I'm trying to utilize a ConstraintLayout (version 1.0.2) to set the height of 2 side-by-side views to match the tallest one of them. This serves as a ViewHolder in for a RecyclerView, where each TextView gets an arbitrary length of text...
如果我将每个都设置为wrap_content,则较短的那个会缩小. 如果我都将其设置为0dp(match_contraints),则两者都将以0高度结束.
If I set each to wrap_content, the shorter one will shrink. If I set both to 0dp (match_contraints), both end up 0 height.
这是设置:
<?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"应该比实际的0 dp更像match_parent.
I suppose this is a bug, as "0dp" should act more like match_parent than actual 0 dp.
顺便说一句,在iOS上,等效的自动布局"规则(设置视图的高度以匹配父视图的顶部和底部)可以产生预期的结果.
On iOS, by the way, the equivalent Auto Layout rules (of setting views' heights to match top and bottom of parent) produce the expected result.
使用LinearLayout当然这很简单,但是此布局在较大的布局中起作用,我想修剪视图层次结构...
Of course this is pretty simple using LinearLayout, but this layout plays part in a larger layout, and I'd like to trim the view hierarchy...
推荐答案
回答,以防将来有人在寻找答案.
Answering, in case anyone is looking out for answer in future.
ConstraintLayout 引入了
ConstraintLayout introduced Barrier in v1.1 to achieve one such functionality asked by the OP in the question
可以使用以下xml来实现上述功能:
The above mentioned functionality can be achieved using below 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>
这篇关于ConstraintLayout:设置行中所有视图的高度以匹配最高的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!