9

我的屏幕ScrollView和三個不同的LinearLayouts如何將屏幕垂直分爲三部分?

其中一個LinearLayout包含Spinner,第二個包含ListView,第三個包含兩個Buttons(水平)。

我想要顯示一個屏幕,其中包含總是顯示在屏幕底部的3 LinearLayouts和總是顯示在頂部的1 LinearLayout。在中間部分,我想顯示ListView的內容。因此整個屏幕上不存在空白區域。

我想創建具有不同大小的多個設備的屏幕。

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/backrepeat" 
android:orientation="vertical" > 

<include 
    android:id="@+id/include1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    layout="@layout/actionbar" > 
</include> 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="horizontal" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:padding="10dp" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight=".2" 
      android:background="@drawable/rounded_border" 
      android:orientation="vertical" 
      android:padding="15dp" > 

      <TextView 
       android:id="@+id/tvDiaryData" 
       style="@style/greenstyle" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/heading" 
       android:gravity="center_horizontal" 
       android:text="@string/tvDiaryData" > 
      </TextView> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="10dp" 
       android:gravity="center_horizontal" > 
      </TextView> 

      <Spinner 
       android:id="@+id/spDiaryAllBooking" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:prompt="@string/select_diaryallbooking" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1.6" 
      android:background="@drawable/layerlist" 
      android:orientation="vertical" 
      android:paddingBottom="5dp" 
      android:paddingLeft="15dp" 
      android:paddingRight="15dp" 
      android:paddingTop="5dp" > 

      <ListView 
       android:id="@+id/lvDiaryBooking" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:divider="#636466" 
        android:layout_weight="1" 
       android:dividerHeight="1dip" 
       android:scrollbarStyle="outsideOverlay" 
       android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight=".2" 
      android:background="@drawable/rounded_border" 
      android:orientation="horizontal" 
      android:padding="10dp" > 

      <Button 
       android:id="@+id/btnDiaryBook" 
       style="@style/greenButton" 
        android:layout_weight="1" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent"      
       android:onClick="btnDiaryBook_Click" 
       android:text="@string/btnBook" > 
      </Button> 

      <Button 
       android:id="@+id/btnDiaryBalance" 
       style="@style/greenButton" 
        android:layout_weight="1" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_marginLeft="10dp"     
       android:onClick="btnDiaryBalance_Click" 
       android:text="@string/btnDiaryBalance" > 
      </Button> 
     </LinearLayout> 
    </LinearLayout> 
     </ScrollView> 

    </LinearLayout> 

屏幕布局與我想要的東西:

enter image description here

Current Layout - This give blank space on below of screen on tablet

回答

21

這是一個非常簡單的解決方案。你應該能夠在你當前的佈局中使用它。

只需填寫您想要的內容LinearLayouts

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

    <LinearLayout 
     android:id="@+id/ll1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/ll2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/ll3" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 
    </LinearLayout> 

</LinearLayout> 

上面貼出的代碼的屏幕截圖顯示了不同佈局區域的顏色。

enter image description here

+1

2名編輯 - 使用「FILL_PARENT」而不是「match_parent」,並嘗試設置您要標準化爲「0dp」 ......在這種情況下,layout_height =「0dp」,而不是layout_height =的尺寸「fill_parent」 – edthethird 2012-08-16 14:33:57

+1

@edthethird很確定fill_parent已被棄用,您仍然應該使用match_parent。他們做同樣的事情。 – 2012-08-16 14:41:48

+0

哦,你是對的,我倒過來了。 – edthethird 2012-08-16 15:07:01