2011-11-25 93 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

迄今爲止我所見過的最佳解決方案。 –