【问题标题】:How to give maximum height as possible to views that are vertically aligned and keep these views with the same height?如何为垂直对齐的视图提供最大高度并保持这些视图具有相同的高度?
【发布时间】:2019-08-10 00:51:57
【问题描述】:

我有三个 ImageView 表示垂直链接的卡片。在它们上面我有一个 TextView,在它们下面有一个 Button。你可以在这里看到我的意思:https://imgur.com/a/jVvR5xC

我希望卡片尽可能高,具体取决于设备的大小。对我来说重要的是所有三张卡片的高度都是动态调整的,但它们之间也保持不变。当然,它们不能与上面的 TextView 或下面的 Button 重叠。

【问题讨论】:

  • 请分享你的xml
  • 分享代码是个好主意,没有它帮不了你:)

标签: android xml imageview android-constraintlayout


【解决方案1】:

使用线性布局作为父视图和方向=垂直。内部线性布局使用卡片视图。为每个卡片视图设置 layout_weight=1。

【讨论】:

  • 这不是回答问题,因为 OP 说他正在使用 constraintlayout 和 vertically chained 视图,他正在寻找约束布局解决方案。永远不要在约束布局中使用嵌套视图
【解决方案2】:

如果您已经有 3 个垂直链接的项目,您只需将 3 个图像的高度声明为匹配约束或0dp:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="This is the top text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="This is the bottom text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintEnd_toEndOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/button2"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4"
        app:layout_constraintVertical_chainStyle="spread" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/textView5"
        app:layout_constraintEnd_toEndOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2012-11-04
    相关资源
    最近更新 更多