滚动视图中的背景未填满整个屏幕 android

Background not filling entire screen in scrollview android

我正在开发一个应用程序,我需要用背景填充整个屏幕,这个视图包含一个滚动视图。问题是背景没有覆盖整个屏幕,底部有一条白色条纹,如下图。

我已经尝试了多种 scaleType 选项,例如 fitXY、centerInside 等。none 成功了。 我的XML在下面。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/signup_background" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="40dp">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/image_view_photo"
                android:layout_width="130dp"
                android:layout_height="130dp"
                android:layout_gravity="center"
                android:layout_marginBottom="25dp"
                android:src="@drawable/camera_icon" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/edit_text_name"
                style="@style/BaseEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/name_icon"
                android:hint="@string/field_name"
                android:inputType="textCapWords" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/edit_text_email"
                style="@style/BaseEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/email_icon"
                android:hint="@string/field_email"
                android:inputType="textEmailAddress" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/edit_text_password"
                style="@style/BaseEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/password_icon"
                android:hint="@string/field_password"
                android:inputType="textPassword" />

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/edit_text_confirm_password"
                style="@style/BaseEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="35dp"
                android:drawableLeft="@drawable/password_icon"
                android:hint="@string/field_confirm_password"
                android:inputType="textPassword" />

            <com.jacksonueda.reachalert.Utils.LoadingButton
                android:id="@+id/btn_signup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:text="@string/action_sign_up" />
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
</ScrollView>

谁能帮帮我?谢谢

我认为你必须给宽度 wrap_contentConstraintLayout 背景。还可以为 ScrollView 和 ConstraintLayout wrap_content.

设置高度

在约束布局中,您必须指定约束。您在 ImageView

中没有指定约束
<ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/signup_background" />

替换为

<ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/signup_background"
            card_view:layout_constraintBottom_toBottomOf="parent"
            card_view:layout_constraintLeft_toLeftOf="parent"
            card_view:layout_constraintRight_toRightOf="parent"
            card_view:layout_constraintTop_toTopOf="parent" />

尝试在 xml 中使用 RelativeLayout 而不是 ConstraintLayout。相对布局最适合重叠 xml.

的两个元素

我得到了答案,我需要做的是将 RelativeLayout 作为 ScrollView 的父级,如下所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/signup_background"
            android:orientation="vertical"
            android:padding="40dp">

            ...

        </LinearLayout>
    </ScrollView>
</RelativeLayout>