我正在尝试使用ConstraintLayout(版本1.0.2)来设置2个并排视图的高度以匹配其中最高的一个。这用作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上,等效的自动布局规则(设置视图的高度以匹配父级的顶部和底部)会产生预期的结果。
当然使用LinearLayout非常简单,但是这个布局在更大的布局中起作用,我想修剪视图层次......
答案 0 :(得分:4)
回答,以防有人在将来寻找答案。
ConstraintLayout在 v1.1 中引入了Barrier,以实现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>
答案 1 :(得分:0)
这可能会对你有帮助。
<android.support.constraint.ConstraintLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/tv_1"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@color/colorAccent"
android:gravity="center"
android:padding="@dimen/activity_vertical_margin"
android:text="sjdjhshdjhdjhsdgfjhgsdjfgjsdgfjsdgfhgdsjhfghs"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tv_2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_2"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="@dimen/activity_vertical_margin"
android:text="sjdjhsjhd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tv_1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 2 :(得分:0)
<TextView
android:id="@+id/txt_service_request_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_rectangle_blue"
android:gravity="center"
android:padding="8dp"
android:text="@string/service_request_id"
android:textColor="@android:color/white"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/txt_service_request_status"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_service_request_status"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/bg_rectangle_blue"
android:gravity="center"
android:padding="8dp"
android:text="@string/service_request_status"
android:textColor="@android:color/white"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/txt_service_request_id"
app:layout_constraintEnd_toStartOf="@+id/txt_service_request_desc"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/txt_service_request_id"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_service_request_desc"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/bg_rectangle_blue"
android:gravity="center"
android:padding="8dp"
android:text="@string/service_request_desc"
android:textColor="@android:color/white"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/txt_service_request_id"
app:layout_constraintEnd_toStartOf="@+id/txt_service_request_cat"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/txt_service_request_status"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_service_request_cat"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/bg_rectangle_blue"
android:gravity="center"
android:padding="8dp"
android:text="@string/service_request_cat"
android:textColor="@android:color/white"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/txt_service_request_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/txt_service_request_desc"
app:layout_constraintTop_toTopOf="parent" />
答案 3 :(得分:0)
这既棘手又缓慢,但是可以。
重点是:
wrap_content
高度属性,因此如果足够大,它将拉伸父级。如您所见,下面的截图中,我们有两种布局。隐藏的是wrap_contents
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/view_1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#DDDDDD"
android:text="@android:string/yes"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/view_2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/view_1_invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#DDDDDD"
android:text="@android:string/yes"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/view_2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#DDDDDD"
android:text="@android:string/httpErrorBadUrl"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/view_1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/view_2_invisible"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#DDDDDD"
android:text="@android:string/httpErrorBadUrl"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/view_1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>