首页 > 解决方案 > 当孩子身高增加时,ScrollView 不滚动

问题描述

我有一个ScrollView包含 2 个片段的。底部片段有 2 个EditText视图。当我在任一 EditText 中输入多行文本时,屏幕上视图的整体高度要求自然会增加。但是,屏幕会切断由于多行文本输入而被推送的任何内容。ScrollView 在那一点上不滚动。

我在 SO 上尝试了一些建议的方法,例如不将 ScrollView 作为根视图或添加一个空视图作为滚动视图的最后一个孙子,但到目前为止它们都没有工作。这是我的 XML:

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

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

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:context=".MainActivity">

            <fragment
                android:id="@+id/fragment1"
                android:name="com.example.omar.memegenerator.TopImageFragment"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="18dp"
                android:layout_marginBottom="18dp"
                tools:layout="@layout/fragment_top_image" />

            <fragment
                android:id="@+id/fragment2"
                android:name="com.example.omar.memegenerator.BottomControlsFragment"
                android:layout_width="match_parent"
                android:layout_height="134dp"
                android:layout_below="@+id/fragment1"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="34dp"
                tools:layout="@layout/fragment_bottom_controls" />

        </RelativeLayout>
    </ScrollView>

</LinearLayout>

标签: androidandroid-layoutandroid-linearlayoutandroid-scrollview

解决方案


将其用于您的布局层次结构。我省略了一些属性,但包含的属性是让它工作的关键。

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

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical">

        <fragment
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

   </LinearLayout>

</ScrollView>

推荐阅读