【发布时间】:2019-11-27 18:56:37
【问题描述】:
我正在尝试为 recyclerview 创建一些布局。我有一个看法。我希望这种观点与它的父母一样长。但由于我将在回收站视图中使用该布局,因此 Parent 必须是包装内容。如果我使该视图匹配父级父级也成为匹配父级。我该如何解决这个问题?
【问题讨论】:
标签: android user-interface view
我正在尝试为 recyclerview 创建一些布局。我有一个看法。我希望这种观点与它的父母一样长。但由于我将在回收站视图中使用该布局,因此 Parent 必须是包装内容。如果我使该视图匹配父级父级也成为匹配父级。我该如何解决这个问题?
【问题讨论】:
标签: android user-interface view
您可以为此使用ConstraintLayout,创建一个视图wrap content/match_parent,然后将另一个视图垂直约束到它+确保此视图具有android:layout_height="0dp",因此他将具有与第二个视图相同的高度, 类似这样的:
<?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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Wrap content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:text="same height"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/button" />
现在,只要您的左键高度发生变化,右键也会发生变化。
下面是它的外观(添加了约束)以便更好地理解:
【讨论】: