Android-在父级包装内容时强制对象与父级匹配

时间:2019-07-18 19:14:12

标签: android user-interface view

我正在尝试为recyclerview创建一些布局。我有一个观点。我认为这种观点与其父观点一样长。但是由于我将在回收者视图中使用该布局,因此父项必须包装内容。如果我使该视图与父对象匹配,则父对象也变为匹配父对象。我该怎么解决?

Example İmage

1 个答案:

答案 0 :(得分:0)

您可以为此使用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" />

现在,每当您的左键高度改变时,右键也会改变。

以下是它的外观(带有附加的约束),用于更好地理解:

enter image description here

相关问题