2014-07-19 69 views
1

我有一个垂直布局作为其子的scrollView。此垂直布局具有背景属性。在这个垂直布局中,我有14个按钮堆叠在一起。它不会结束最后一个按钮底部的背景,而是继续添加相当多的我不想要的空间。这是XML文件的代码。Android ScrollView在页面底部留下额外的空间

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/mainbg" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/maps" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/battingcages" /> 

     <Button 
      android:id="@+id/collec" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/thebeyond" /> 

     <Button 
      android:id="@+id/mut" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/css" /> 

     <Button 
      android:id="@+id/ach" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/csite" /> 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/crane" /> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/gas" /> 

     <Button 
      android:id="@+id/button7" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/tif" /> 

     <Button 
      android:id="@+id/button8" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/lgtf" /> 

     <Button 
      android:id="@+id/button9" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/pent" /> 

     <Button 
      android:id="@+id/button10" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/slides" /> 

     <Button 
      android:id="@+id/button11" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/treehouse" /> 

     <Button 
      android:id="@+id/button12" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/space" /> 

     <Button 
      android:id="@+id/button13" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/fight" /> 

     <Button 
      android:id="@+id/button14" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/tower" /> 

    </LinearLayout> 

</ScrollView> 
+1

什么是绘制mainbg?是9.png吗?可绘制的XML资源?尝试把它拿出来,只是设置颜色为红色的东西,看看会发生什么。 – ksarmalkar

+0

布局没问题。你能显示一个屏幕截图吗? – eleven

+0

这是一个普通的.jpg,但后来我将它复制到draw9patch工具中并保存。不用找了。这里有一个截图:http://i.imgur.com/yghJXV9.png。另外我没有这个问题,当不使用scrollView和.jpg – Josh

回答

2

您可以用另一个包含布局包装您的ScrollView,然后将您的背景可绘制分配给该布局。

编辑:

另一种解决方案

放置在一个 '绘制-nodpi' 文件夹你的背景绘制。这将导致系统不能缩放图像,以及关于支持多种显示尺寸的明显警告。

说明

发生了什么事是你的背景图像被放大到匹配显示器的密度,这增加了该视图的最小高度,因此,滚动型的高度。这会造成不需要的滚动。将ScrollView放在一个包含的布局中,并指定可绘制的背景,可以缓解这个问题,因为它显然不会滚动。然而,缩放的另一个可能的副作用是它可能会创建位图太大而不能显示android(超过4096x4096)。这只会在运行时发出警告,并且不会呈现位图,这对您的用户来说显然是一种糟糕的体验。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/mainbg"> 

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

    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mainbg" 
      android:orientation="vertical"> 

     <Button 
       android:id="@+id/maps" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/battingcages"/> 

     <Button 
       android:id="@+id/collec" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/thebeyond"/> 

     <Button 
       android:id="@+id/mut" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/css"/> 

     <Button 
       android:id="@+id/ach" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/csite"/> 

     <Button 
       android:id="@+id/button5" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/crane"/> 

     <Button 
       android:id="@+id/button6" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/gas"/> 

     <Button 
       android:id="@+id/button7" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/tif"/> 

     <Button 
       android:id="@+id/button8" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/lgtf"/> 

     <Button 
       android:id="@+id/button9" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/pent"/> 

     <Button 
       android:id="@+id/button10" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/slides"/> 

     <Button 
       android:id="@+id/button11" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/treehouse"/> 

     <Button 
       android:id="@+id/button12" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/space"/> 

     <Button 
       android:id="@+id/button13" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/fight"/> 

     <Button 
       android:id="@+id/button14" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/tower"/> 

    </LinearLayout> 

</ScrollView> 

+0

大部分工作。我现在唯一的问题是按钮不会填满屏幕的宽度,即使scrollView被设置为填充父宽度以及相对布局。 – Josh

相关问题