【问题标题】:Unable to scroll Linear Layout with ScrollView无法使用 ScrollView 滚动线性布局
【发布时间】:2018-10-17 20:23:13
【问题描述】:

我是 Android 新手。我正在创建一个表单来添加用户。由于表单字段很多,我需要向下滚动。我读到 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/>

【问题讨论】:

  • 您的问题是否解决了?

标签: android scrollview android-linearlayout


【解决方案1】:

使用下面的代码

<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>  

【讨论】:

    【解决方案2】:

    将滚动视图的 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">
    

    【讨论】:

      【解决方案3】:

      你需要添加一些属性:

      1) 到您的 ScrollView:

      android:fillViewport="true"
      

      2) 到你的 LinearLayout:

      android:descendantFocusability="blocksDescendants"
      

      例子:

      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-04
        • 1970-01-01
        • 1970-01-01
        • 2014-08-03
        相关资源
        最近更新 更多