【问题标题】:imageView causes parent layout to shrink in constraint layoutimageView 导致父布局在约束布局中收缩
【发布时间】:2023-04-02 17:43:01
【问题描述】:

添加 imageView 时,约束布局中的布局会缩小。预览按预期显示。如果没有 imageView,则布局与父宽度匹配。

    <?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="wrap_content">

<TextView
    android:id="@+id/crime_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:background="@color/cardview_light_background"
    android:text="Crime Title"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/crime_date"
    app:layout_constraintEnd_toStartOf="@+id/crime_solved"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/crime_date"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="8dp"
    android:padding="2dp"
    android:text="Crime Date"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/crime_title" />

<ImageView
    android:id="@+id/crime_solved"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="0dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="7dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_solved"
    tools:adjustViewBounds="true" />


</android.support.constraint.ConstraintLayout>

![问题截图]https://imgur.com/gallery/UwrCdsq

【问题讨论】:

  • 提供完整的xml

标签: android android-layout android-imageview


【解决方案1】:

ImageView的高度和宽度设置为0dp并设置如下属性

app:layout_constraintWidth_default="wrap"
app:layout_constraintHeight_default="wrap"

同时从您的TextView 中删除不必要的margins

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2020-06-09
    • 2020-02-29
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    相关资源
    最近更新 更多