ScrollView dos not work

扶醉桌前 提交于 2019-12-25 02:37:08

问题


Dear All experts I have problem with using scrollView anyone please help.. the Error is showing that the ScrollView is useless and also in other form of mine is shows the same , how can I solve this and how can I make my forms and activities been scroll ??

`

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:text="@string/welcome"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_margin="30dp"
    android:contentDescription="@+id/button1"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/username"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        style="android:buttonStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="goToMain"
        android:text="@string/log_in" />

    <Button
        android:id="@+id/button2"
        style="android:buttonStyle "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="register"
        android:text="@string/registration" />
</LinearLayout>
</ScrollView>

`


回答1:


You need a child container as LinearLayout, TableLayout or RelativeLayout in your ScrollView like this:

<ScrollView 
    ... >

    <LinearLayout
        ... >

        <!-- Your views: TextView, LinearLayout, etc. -->

    </LinearLayout>

</ScrollView>

According to the reference:

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll




回答2:


You should put all the other fields in a single Layout.. Like linear layout,relative layout. Your whole code should look like this

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

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

        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:text="@string/welcome"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_gravity="center"
            android:layout_margin="30dp"
            android:contentDescription="@+id/button1"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/username"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="text" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/password"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPassword" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="vertical" >

            <Button
                android:id="@+id/button1"
                style="android:buttonStyle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:onClick="goToMain"
                android:text="@string/log_in" />

            <Button
                android:id="@+id/button2"
                style="android:buttonStyle "
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:onClick="register"
                android:text="@string/registration" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>



回答3:


"Scrollview can host only one direct child"

Put all your stuff in some Layout e.g LinearLayout



来源:https://stackoverflow.com/questions/22269328/scrollview-dos-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!