imageView导致父布局在约束布局中缩小

时间:2018-11-20 00:23:55

标签: android android-layout android-imageview

添加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

1 个答案:

答案 0 :(得分:0)

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

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

还从您的margins中删除不必要的TextView