2011-11-25 33 views
19

我有以下的XML。我试图在我的ScrollView下创建一个静态按钮。我尝试设置权重,将按钮设置为在滚动视图下方等。有人可以给我一种方法,让我可以让按钮保持在底部,滚动视图只占据屏幕中间位置?如何在ScrollView下创建一个静态按钮?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 



    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/menuButtons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <Button xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/newItems" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Items" /> 
     <Button xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/categories" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Categories" /> 

    </LinearLayout> 


    <ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/contentScroller" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/menuButtons"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 


      <TableLayout 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:stretchColumns="1" > 


       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip" > 
        <ImageView 
         android:src="@drawable/thumbdrive"/>" 
        <TextView 
         android:layout_column="1"    
         android:text="Thumb Drives"    
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <ImageView 
         android:src="@drawable/laptop"/> 
        <TextView 
         android:layout_column="1" 
         android:text="Laptops" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <ImageView 
         android:src="@drawable/sdcard"/> 
        <TextView 
         android:layout_column="1" 
         android:text="SD Cards" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
       <TableRow android:layout_marginTop="5dip" 
          android:layout_marginBottom="5dip"> 
        <TextView 
         android:layout_column="1" 
         android:text="Other" 
         android:padding="3dip" 
         android:textSize="20dip"/> 
       </TableRow> 
      </TableLayout> 
     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/submit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Submit New Item" 
     android:layout_below="@id/contentScroller"/> 

</RelativeLayout> 
+0

我昨天解决了完全相同的问题,但我发现了另一个技巧,只是好奇心,你能告诉我在横向模式下的视觉效果如何?谢谢 – tuandroid

+0

风景很好。 – kieblera5

回答

29

使用RelativeLayout。从底部的Button开始,并将ScrollView定位在Button之上。

Relative Layout - Android Developers

<RelativeLayout 
    (...)> 

    <LinearLayout android:id="@+id/ll1" 
     android:layout_alignParentTop="true" 
     (...)/> 

    <Button android:id="@+id/button" 
     android:layout_alignParentBottom="true" 
     (...)/> 

    <ScrollView 
     android:layout_above="@id/button" 
     android:layout_below="@id/ll1" 
     (...)/> 

</RelativeLayout> 

这样的事情。写在我的头上,所以可能会发生一些错误。

+0

谢谢。我之前曾尝试过这种方法,但是我在scrollview之后使用了按钮,所以它不起作用,但它的工作原理与您编写它的方式相同。 – kieblera5

+1

这将导致'RelativeLayout'向下延伸到其容器的底部。这可能是好的。 _注意你不能在RelativeLayout的大小和子级的位置之间有循环依赖。例如,您不能拥有一个RelativeLayout,其高度设置为WRAP_CONTENT,并且子设置为ALIGN_PARENT_BOTTOM._ - [RelativeLayout Reference](http://developer.android.com/reference/android/widget/RelativeLayout.html) – jtooker

+0

完美一个为我的问题...(y) – Elizabeth

0

发生在一个RelativeLayout的按钮并使其与父底部:

<RelativeLayout android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:orientation="horizontal" 
    android:layout_height="50px"> 
<Button 
    android:id="@+id/submit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="Submit New Item" 
    android:layout_below="@id/contentScroller"/> 
</RelativeLayout> 
48

我知道这是真的晚了,但有一个更好的解决方案。 RelativeLayout方法的问题以及将按钮与相对布局的底部对齐的原因是它强制布局高度基本上与fill_parent(或match_parent)相同。

正确的方法做,这是如下:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:fillViewport="true"> 
      <!-- Your Scrollview content goes here --> 
    </ScrollView> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center_horizontal" 
     android:text="Button Text Goes Here" /> 
</LinearLayout> 

关键是要高度设置为0,然后给它一个布局重量的1 ...从我可以从阅读上来讲在布局大小调整过程中,给它一个0的大小,并且布局权重会导致它阻止调整视图的大小,直到它处理完布局中的所有子元素为止......然后它会在第二遍时出现,并且能够正确调整滚动视图的大小。

+3

非常感谢!为我完美工作(我还没有掌握相关布局)。 – IanB

+0

谢谢。 如果“ScrollView”的内容很少(很短),但是确实正确显示了(外部)'LinearLayout',则**不会将**扩展到其容器底部'Button'下面没有压缩到'1px'(就像你使用'RelativeLayout'并让'Button'在'ScrollView'下面一样)。 如果您不关心“LinearLayout”的所有方式(即始终拉伸滚动区域以尽可能多地占用空间),其他一些解决方案对我来说似乎更加清晰。 – jtooker

+3

迄今为止我所见过的最佳解决方案。 –