2011-05-09 53 views
2

我想將屏幕分爲2個垂直段。底部工具欄應該是固定的 - 比方說,無論如何,我都希望LinearLayout保持底部。佈置屏幕。需要工具欄留在底部

頂部 - 我想要ScrollView,它將長大到工具欄,然後允許滾動。否則 - 它可以是完全空的,但工具欄仍然需要在底部。

我該如何做到這一點?

回答

7

一如既往,有幾種方法。我會做以下

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 
     <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1"> 
     </ScrollView> 
     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent"> 
     <Button 
      android:text="Button1" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"/> 
     <Button 
      android:text="Button2" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"/> 
     </LinearLayout> 
</LinearLayout> 

這將使用垂直線性佈局,並提出使用WRAP_CONTENT底部的按鈕,然後給它的「1」的權重給出了滾動的空間休息。

+0

如果我可以給這個答案1000 upvotes,我會的。我一直在爭取讓這個佈局能夠正常工作一段時間(對於android佈局還是有點新鮮的)。這很好。 – 2014-09-07 09:49:17

0

這是實現它的一種方法,關鍵是第二個LinearLayout中的android:layout_alignParentBottom =「true」,這裏有兩個按鈕。

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

    <ScrollView android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:layout_marginTop="5dp" 
     android:layout_marginBottom="60dp" android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp"> 

     <LinearLayout android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="wrap_content"> 

      <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" android:text="Some text" /> 

     </LinearLayout> 
    </ScrollView> 

    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:paddingTop="5dp" 
     android:layout_alignParentBottom="true" android:layout_marginTop="5dp"> 

     <Button android:id="@+id/ok_button" android:layout_width="fill_parent" 
      android:layout_weight="1" android:layout_height="wrap_content" 
      android:text="OK" android:layout_alignParentLeft="true" /> 

     <Button android:id="@+id/cancel_button" android:layout_width="fill_parent" 
      android:layout_weight="1" android:layout_height="wrap_content" 
      android:text="Cancel" android:layout_toRightOf="@id/ok_button" /> 
    </LinearLayout> 
</RelativeLayout> 
相關問題