Search code examples
androidandroid-constraintlayoutandroid-designer

Image have margin from top in constraintlayout


I have a scrolled content, so I put a constraint layout inside a scrollview, I have a problem when put an image inside a constraint layout, but the image show a space from top such as in screenshot below, how can I remove that space?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <!-- have whatever children you want inside -->
        <ImageView
                app:srcCompat="@drawable/main_header"
                android:id="@+id/img_icon"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="#ffccbb"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_percent="1"
                android:contentDescription="TODO" app:layout_constraintDimensionRatio="1:1"
                app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.0"/>
    </android.support.constraint.ConstraintLayout>
</ScrollView>

screenshot


Solution

  • In your Imageview you have added app:layout_constraintDimensionRatio="1:1" that means it will take space 1:1, so width and height also will be 1:1. But your image app:srcCompat="@drawable/main_header" is not 1:1. So you have to add an image which has width and height of the same size. Otherwise, you can add android:scaleType="fitXY" or android:scaleType="centerCrop" according to your expectation.