2011-05-23 74 views
3

我想在兩個ListViews在一個屏幕垂直一個在另一個下面,也是兩個ListView同樣需要50%-50%的屏幕。我的問題是,我創建了佈局,在eclipse(圖形佈局)看起來不錯,但在設備中它不會佔據屏幕的一半。這似乎取決於ListView的內容。我怎麼能這樣做?Android的兩個listview垂直使用50%-50%的屏幕高度

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

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dip" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     android:gravity="left" 
     android:textColor="#000" 
     android:text="Some Text" /> 

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_weight="0.5" 
      android:background="#FB0" 
      android:scrollbars="none" 
      android:dividerHeight="2dip" /> 

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_weight="0.5" 
      android:background="#FB0" 
      android:scrollbars="none" 
      android:dividerHeight="2dip" /> 
</LinearLayout> 
+0

你試過更換'機器人:layout_height = 「FILL_PARENT」'和'機器人:layout_height = 「0dp」'兩個? – ernazm 2011-05-23 08:05:24

回答

10

您應該出臺LinearLayout和使用layout_weight,同時設定layout_height="0dp"

下面是一個例子:

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

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

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.5" /> 

     <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.5" /> 

</LinearLayout> 
+0

謝謝,我錯過了這種情況:)我嘗試了單獨的線性佈局,我爲他們設置了重量,現在它似乎工作。 – Alex 2011-05-23 10:32:08

+1

它不工作。當兩個佈局的大小基本相同時,我有一個最好的情況,但如果第一個listview包含更多的元素,那麼高度就會增加。有趣的圖形佈局設計師表示,兩者僅使用半空間。 – Alex 2011-06-01 07:57:41

+0

你可以請你更新你的問題與你目前的佈局,並添加你看到的屏幕截圖? – inazaruk 2011-06-01 08:08:12

相關問題