ScrollView 中的 RelativeLayout 不会滚动。我做错了什么?



我正在尝试使可滚动的景观布局。我将RelativeLayout放在ScrollView中,但什么也不会发生。这是代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:gravity="center_horizontal">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/green_light"
    tools:context="app.example.ui.SplashActivity">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/parent_center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/logo" />
    <View
        android:id="@+id/parent_center"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />
    <LinearLayout
        android:id="@+id/ll_gateway_discovery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/parent_center"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/registration"
            android:textSize="@dimen/text_large"
            android:textStyle="bold" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/who_are_you"
            android:textColor="@color/grey_dark"
            android:textSize="@dimen/text_normal"
            android:textStyle="bold" />
        <EditText
            android:id="@+id/et_username"
            android:layout_margin="8dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="240dp"
            android:hint="@string/prompt_email"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:singleLine="true"
            android:textSize="@dimen/text_normal" />
        <EditText
            android:id="@+id/et_password"
            android:layout_margin="8dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="240dp"
            android:imeActionId="@+id/login"
            android:imeActionLabel="@string/action_sign_in_short"
            android:imeOptions="actionUnspecified"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"
            android:text="pippo"
            android:hint="@string/password"
            android:textSize="@dimen/text_normal" />
        <ImageView
            android:id="@+id/btn_login"
            android:layout_marginTop="4dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_submit" />
    </LinearLayout>
</RelativeLayout>

我在做错什么?它使我发疯:)

使用linearlayout代替relativelayout

使用 HorizontalScrollView而不是 ScrollView,因为普通卷轴是垂直滚动

和scrollview child在宽度/高度上包裹着。

问题是您使用的是match_parent,而不是wrap_content作为layout_width和layout_height for the Relativelayout。例如,如果您想垂直滚动,则必须使用此信息:

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

使用此相关性将具有与卷轴相同的宽度,但高度将与其中的视图一样大。

结果XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:gravity="center_horizontal" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green_light"
        tools:context="app.example.ui.SplashActivity" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/parent_center"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/logo" />
        <View
            android:id="@+id/parent_center"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_centerInParent="true" />
        <LinearLayout
            android:id="@+id/ll_gateway_discovery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/parent_center"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:orientation="vertical" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/registration"
                android:textSize="@dimen/text_large"
                android:textStyle="bold" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/who_are_you"
                android:textColor="@color/grey_dark"
                android:textSize="@dimen/text_normal"
                android:textStyle="bold" />
            <EditText
                android:id="@+id/et_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:hint="@string/prompt_email"
                android:inputType="textEmailAddress"
                android:maxLines="1"
                android:minWidth="240dp"
                android:singleLine="true"
                android:textSize="@dimen/text_normal" />
            <EditText
                android:id="@+id/et_password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:hint="@string/password"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:inputType="textPassword"
                android:maxLines="1"
                android:minWidth="240dp"
                android:singleLine="true"
                android:text="pippo"
                android:textSize="@dimen/text_normal" />
            <ImageView
                android:id="@+id/btn_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:background="@drawable/btn_submit" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

最新更新