简体   繁体   English

无法使用ScrollView滚动线性布局

[英]Unable to scroll Linear Layout with ScrollView

I am new to Android. 我是Android新手。 I am creating a form to add users. 我正在创建一个添加用户的表单。 As the form fields are many, I need to scroll down. 由于表单字段很多,我需要向下滚动。 I read that the ScrollView takes only one child view. 我读到ScrollView仅采用一个子视图。 I have wrapped my view inside a Linear Layout which is the direct child of the ScrollView. 我已经将视图包装在线性布局中,这是ScrollView的直接子级。

<ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorWhite"
                android:animateLayoutChanges="true"
                android:scrollbars="none">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

                <EditText... />

                <EditText.../>

                <EditText... />

                <EditText.../>

                <LinearLayout... >

                <LinearLayout... >

                <LinearLayout... >

                <TextView... />
     </LinearLayout>
 <ScrollView/>

Change the xml of your scrollview to this 将滚动视图的xml更改为此

<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="wrap_content"
        android:orientation="vertical">

You need to add some attributes: 您需要添加一些属性:

1) To your ScrollView: 1)到您的ScrollView:

android:fillViewport="true"

2) And to your LinearLayout: 2)和您的LinearLayout:

android:descendantFocusability="blocksDescendants"

Example: 例:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardDetailScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/card_detail_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants">

        Your Content...

    </LinearLayout>
</ScrollView>

Use below code 使用以下代码

<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android"  
     xmlns:tools="http://schemas.android.com/tools" 
     android:fillViewport="true"    
    android:layout_marginTop="30dp"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:id="@+id/scrollView">  


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

        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 1" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 2" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 3" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 4" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 5" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 6" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 7" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 8" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 9" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 10" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 11" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 12" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 13" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 14" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 15" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 16" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 17" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 18" />  

        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 19" />  
        <Button  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Button 20" />  

    </LinearLayout>  

</ScrollView>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM