【问题标题】:scrollview leaves extra space at bottom of layout activityscrollview 在布局活动的底部留下额外的空间
【发布时间】:2016-08-31 08:10:14
【问题描述】:

我的Scrollview 在一些layouts 下留下空间我是android 新手,请帮助这里是我的代码:

 <ScrollView     
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollviewdb"     
    android:layout_width="match_parent"    
    android:layout_height="wrap_content" >  
    <LinearLayout    
      xmlns:android="http://schemas.android.com/apk/res/android" 
              android:orientation="vertical"    
              android:layout_width="match_parent"    
              android:layout_height="wrap_content">    
         <TextView        
            android:layout_width="match_parent"   
            android:layout_height="wrap_content"         
            android:textSize="15sp"  
            android:padding="5dp"     
            android:id="@+id/ghavaed_text"/> 
      </LinearLayout>
  </ScrollView>

【问题讨论】:

  • 添加截图..这样人们可以帮助你..
  • 欢迎来到 Stack Overflow!请带上tour,四处看看,通读help center,尤其是How do I ask a good question?
  • 在滚动视图中使用 android:fillViewport="true"
  • 它对我不起作用我在@PradeepGupta 之前尝试过

标签: android android-layout android-scrollview


【解决方案1】:

在您的 ScrollView 中添加 android:fillViewport

<ScrollView     
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollviewdb"     
    android:fillViewport="true"
    android:layout_width="match_parent"    
    android:layout_height="wrap_content" >  
    <LinearLayout    
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical"    
      android:layout_width="match_parent"    
      android:layout_height="wrap_content">    
         <TextView        
            android:layout_width="match_parent"   
            android:layout_height="wrap_content"         
            android:textSize="15sp"  
            android:padding="5dp"     
            android:id="@+id/ghavaed_text"/> 
    </LinearLayout>
</ScrollView>

正如@Ironman 解释的那样,这背后的原因是,如果 ScrollView 的内容总是和它自己一样高,它就会变得毫无用处。要解决此问题,您需要使用名为 android:fillViewport 的 ScrollView 属性。当设置为 true 时,如果需要,此属性会使滚动视图的子视图扩展到 ScrollView 的高度。当child高于ScrollView时,该属性无效

【讨论】:

  • 我给你+1,因为你的答案是正确的,但你必须添加解释ScrollView would become useless if its content was always as tall as itself. To work around this, you need to use the ScrollView attribute called android:fillViewport. When set to true, this attribute causes the scroll view’s child to expand to the height of the ScrollView if needed. When the child is taller than the ScrollView, the attribute has no effect
  • fillViewport 无法解决 :( @MahfuzIslamBhuiyan
  • @Erik_DA 请添加屏幕截图。
【解决方案2】:

我遇到了同样的问题,并通过在ScrollView 内的LinearLayout 中添加:android:layout_gravity="top" 来解决它。

【讨论】:

    【解决方案3】:

    您可以在滚动视图上添加带有负值的 marginBottoom。

    在我的例子中,我发现背景高度在底部占用了更多空间。 解决方案:我已经将背景从相对布局移到滚动视图中,已经解决了这个问题。

    <ScrollView 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"
    tools:context=".MainActivity"
    android:fillViewport="true"
    android:background="@drawable/bg" //i move this background here from realative 
        layout
    android:overScrollMode="never"
    >
     <RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
      </RelativeLayout>
      </ScrollView>
    

    希望这能解决您的问题

    【讨论】:

      【解决方案4】:

      在滚动视图中使用android:fillViewport 属性来设置滚动视图的高度和宽度填充端口(匹配父级)。如果我们不在滚动视图中使用这个属性,它的行为就像一个 wrap_content

      代码:

       <ScrollView     
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/scrollviewdb"     
      android:layout_width="match_parent"    
      android:layout_height="wrap_content"
       android:fillViewport="true" >  
      <LinearLayout    
        xmlns:android="http://schemas.android.com/apk/res/android" 
                android:orientation="vertical"    
                android:layout_width="match_parent"    
                android:layout_height="wrap_content">    
           <TextView        
              android:layout_width="match_parent"   
              android:layout_height="wrap_content"         
              android:textSize="15sp"  
              android:padding="5dp"     
              android:id="@+id/ghavaed_text"/> 
        </LinearLayout>
      

      【讨论】: