【问题标题】:Android - Force object to match parent while parent is wrap contentAndroid - 在父对象包装内容时强制对象匹配父对象
【发布时间】:2019-11-27 18:56:37
【问题描述】:

我正在尝试为 recyclerview 创建一些布局。我有一个看法。我希望这种观点与它的父母一样长。但由于我将在回收站视图中使用该布局,因此 Parent 必须是包装内容。如果我使该视图匹配父级父级也成为匹配父级。我该如何解决这个问题?

Example İmage

【问题讨论】:

    标签: android user-interface view


    【解决方案1】:

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

    现在,只要您的左键高度发生变化,右键也会发生变化。

    下面是它的外观(添加了约束)以便更好地理解:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-02
      • 2012-12-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      相关资源
      最近更新 更多