2011-08-07 67 views
0

我有四個LinearLayouts,最頂層的一個作爲包裝,其餘包含對象。我希望中心(用ListView)來填充我的屏幕。所以,我應該看到button1,然後是可滾動的項目列表,然後是屏幕底部的button2。如何安排三個線性佈局?

如果我嘗試設置LinearLayout或ListView的layout_height屬性,我會丟失button2。

也許,我甚至不應該使用LinearLayouts?任何建議,非常感謝!我的佈局XML如下。

<?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"> 
<LinearLayout android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <Button android:text="Button" android:id="@+id/button1" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
</LinearLayout> 

<LinearLayout android:id="@+id/linearLayout3" 
    android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <ListView android:id="@+id/android:list" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     xmlns:android="http://schemas.android.com/apk/res/android" /> 
</LinearLayout> 


<LinearLayout android:id="@+id/linearLayout2" 
    android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <Button android:text="Button" android:id="@+id/button2" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_gravity="bottom"></Button> 
</LinearLayout> 

</LinearLayout> 

回答

1

使用這樣的

<?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"> 

<Button android:text="Button" android:id="@+id/button1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

<ListView android:id="@+id/android:list" 
    android:layout_width="fill_parent" android:layout_height="0dp" 
    android:layout_weight="1.0" /> 

<Button android:text="Button" android:id="@+id/button2" 
    android:layout_width="match_parent" android:layout_height="wrap_content" 
    android:layout_gravity="bottom"></Button> 

</LinearLayout> 
+0

謝謝!這就是訣竅! – LTMOD

2

爲什麼不嘗試擺脫包裝每個項目的線性佈局,並將最外側的LinearLayout設置爲android:orientation =「vertical」?這樣一切都會按照你的想法堆疊起來。

0

您linearLayout3改成這樣:

<LinearLayout android:id="@+id/linearLayout3" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:layout_weight="1.0"> 
    <ListView android:id="@+id/android:list" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
     xmlns:android="http://schemas.android.com/apk/res/android" /> 
</LinearLayout> 

基本定layoutHeight爲 'match_parent',並加入 'layout_weight' 到 '1.0'。

+0

這將刪除此LinearLayout中下面的所有元素。 – PravinCG

0
 <?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"> 

    <Button android:text="Button" 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

     <ListView android:id="@+id/android:list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 

     <Button android:text="Button" 
       android:id="@+id/button2"   
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"   
       android:layout_gravity="bottom"/> 

      </LinearLayout> 

做這樣....