【问题标题】:Aligning a ChipGroup with an ImageView using ConstraintLayout使用 ConstraintLayout 将 ChipGroup 与 ImageView 对齐
【发布时间】:2020-06-04 12:43:03
【问题描述】:

我想在 ImageView 旁边居中对齐 ChipGroup。这是我的 XML:

...

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

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/locationIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title"
            app:srcCompat="@drawable/ic_location_on_black_24dp" />

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/locationChips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            app:layout_constraintBottom_toBottomOf="@+id/locationIcon"
            app:layout_constraintStart_toEndOf="@id/locationIcon"
            app:layout_constraintTop_toTopOf="@id/locationIcon">

        </com.google.android.material.chip.ChipGroup>

    </androidx.constraintlayout.widget.ConstraintLayout>

...

预期结果:

实际结果:

如您所见,芯片没有对齐,而是位于标题文本的顶部,如果文本有一定长度,最后一个芯片也会溢出。我怎样才能解决这个问题?另外,我发现如果 ChipGroup 被限制为父级,最后一个芯片不会溢出。

【问题讨论】:

  • 添加您期望的输出图像
  • @backpack :添加了答案。让我知道您想要更改的任何内容

标签: android android-layout android-constraintlayout android-chips


【解决方案1】:

请移除 ChipGroup 底部约束并应用宽度 = "0dp" 的末端约束

如下图所示。

<?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/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/locationIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title"
        app:srcCompat="@drawable/ic_icon_location" />

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/locationChips"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/locationIcon"
        app:layout_constraintTop_toTopOf="@+id/locationIcon">

</com.google.android.material.chip.ChipGroup>

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 谢谢。这已经修复了芯片溢出问题,但图标仍然没有对齐:imgur.com/fOZcfxd
  • 芯片的图标来自谷歌还是你定制的?
  • 来自 Google。
  • 尝试一件事给chipGroup这个约束:app:layout_constraintTop_toBottomOf="@+id/title" 这个约束给imageview app:layout_constraintTop_toTopOf="@+id/locationChips"
  • 是一样的。没有变化。
猜你喜欢
  • 2020-03-25
  • 2017-12-03
  • 2017-04-23
  • 2012-03-11
  • 2021-10-23
  • 2013-12-14
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多