图片在约束布局中从上到下具有边距

时间:2019-05-26 16:06:18

标签: android android-constraintlayout android-designer

我有一个滚动的内容,所以我将约束布局放在了滚动视图中,将图像放在约束布局中时遇到了问题,但是图像从顶部显示了一个空间,例如下面的屏幕快照,我该如何删除该空间?

<?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

3 个答案:

答案 0 :(得分:0)

在您的Imageview中,您添加了app:layout_constraintDimensionRatio="1:1",这意味着它将占用1:1的空间,因此宽度和高度也将是1:1。但是您的图片app:srcCompat="@drawable/main_header"不是1:1。因此,您必须添加宽度和高度相同的图像。否则,您可以根据自己的期望添加android:scaleType="fitXY"android:scaleType="centerCrop"

答案 1 :(得分:0)

我刚刚在手机上检查了您的验证码,看起来不错。  我与您不同的一件事是,我为imageView使用了不同的图像资源。

因此,我认为您实际上没有问题,我认为这只是图像及其外观,请尝试更改图像资源并查看是否仍然存在此问题。

他是我的布局文件的样子:

<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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/main_header"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

答案 2 :(得分:0)

尝试此操作,删除app:layout_constraintTop_toTopOf="parent"中的ImageView即可!