ConstraintLayout无法缩小链式ImageView

时间:2018-06-17 12:08:40

标签: android android-layout imageview android-constraintlayout

我创建了一个包含两个ImageView和一个TextView的简单布局。 ImageViews需要水平放置在彼此旁边。我尝试使用扩展链来实现这一点,但ImageViews无法缩小以适应屏幕。

enter image description here

修改

当我使用嵌套的LinearLayout时,它可以正常工作。

enter image description here

ImageViews完全可见。

布局:

<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:id="@+id/logo_constraint_layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".LogoActivity">

<ImageView
    android:id="@+id/logo_image_logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/logo_image_logo_content_description"
    android:src="@drawable/logo"
    android:scaleType="centerInside"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/logo_text_view_continue_message"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@id/logo_image_name"
    />

<ImageView
    android:id="@+id/logo_image_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/logo_image_name_content_description"
    android:src="@drawable/name"
    android:scaleType="centerInside"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/logo_text_view_continue_message"
    app:layout_constraintStart_toEndOf="@id/logo_image_logo"
    app:layout_constraintEnd_toEndOf="parent"
    />

<TextView
    android:id="@+id/logo_text_view_continue_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/logo_margin_bottom_continue_message"
    android:fontFamily="@font/raleway_regular"
    android:text="@string/logo_continue_message"
    android:textColor="@color/material_grey"
    android:textSize="@dimen/logo_text_size_continue_message"
    android:visibility="invisible"
    app:layout_constraintTop_toBottomOf="@id/logo_image_logo"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

如果我对您的需求没有错,请尝试下面的布局。

<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"
tools:context=".DemoActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="yellow"
    android:textAlignment="gravity"
    android:textAllCaps="false"
    android:textSize="23sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.437"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toStartOf="@+id/imageView3"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/btn_dialog" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/imageView2"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/btn_dialog" />
 </android.support.constraint.ConstraintLayout>

使用app:layout_constraintHorizontal_chainStyle="spread" Images Views两者将共享相同的权重。

答案 1 :(得分:0)

更改

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_width="0dp"
android:layout_height="0dp"

ImageViews上。 wrap_content强制缩放,而match_constraint0dp)会允许图片根据需要增长/缩小。