ScrollView在布局活动底部留下额外的空间。

6

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

1
添加截图,这样其他人可以帮助你。 - Mujammil Ahamed
1
欢迎来到Stack Overflow!请参观我们的tour,四处浏览,并详细阅读我们的help center,尤其是《如何提出一个好问题?》(http://stackoverflow.com/help/how-to-ask)。 - Squazz
在ScrollView中使用android:fillViewport="true"。 - Pradeep Gupta
它对我没用,我之前尝试过了 @PradeepGupta - Erik_DA
4个回答

6
在你的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的内容始终与其本身一样高,则ScrollView将变得无用。为了解决这个问题,您需要使用ScrollView属性,称为android:fillViewport。当设置为true时,此属性会导致滚动视图的子项在需要时扩展到ScrollView的高度。当子项高于ScrollView时,该属性没有效果。


1
我给你点赞,因为你的答案是正确的,但你需要添加解释:如果ScrollView的内容始终与其本身一样高,则它将变得无用。为了解决这个问题,您需要使用名为android:fillViewport的ScrollView属性。当设置为true时,此属性会导致滚动视图的子项在需要时扩展到ScrollView的高度。当子项高于ScrollView时,该属性无效。 - Harshad Pansuriya
1
这个问题不会通过 fillViewport 得到解决 :( @MahfuzIslamBhuiyan - Erik_DA
@Erik_DA 请添加屏幕截图。 - Harshad Pansuriya

3

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


1

你可以在scrollview上使用负值添加marginBottom。

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

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

希望这能解决您的问题。

0
在ScrollView中使用android:fillViewport属性来设置滚动视图的高度和宽度填充端口(与父容器匹配)。如果我们不在ScrollView中使用此属性,则其行为类似于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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接