2012-07-06 58 views
1

我的應用看起來應該是這樣的: This is how it should look, though the way I had it before it wasn't scaling well. So that's why I am doing this, but it is giving me lots of problems. 這就是它應該看起來的樣子,儘管它在擴展性不好之前的樣子。所以這就是爲什麼我這樣做,但它給了我很多問題。Android layout_weight不一致

但是,甚至看似相同的設置,第二行佔據了所有的房間,使它看起來像這樣: Any explanation why it is doing this? It beats me.

任何解釋它爲什麼這樣做呢?它擊敗了我。 這是我的XML,任何解決方案?

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout02" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:weightSum="7.0" 
> 

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

<ImageView 
    android:id="@+id/terranlogo1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/terranlogo" /> 

<ImageView 
    android:id="@+id/protosslogo1" 
    android:layout_width="wrap_content" 
    android:layout_weight="1.0" 
    android:layout_height="wrap_content" 
    android:src="@drawable/protosslogo" /> 

<ImageView 
    android:id="@+id/zerglogo1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/zerglogo" /> 
</LinearLayout> 

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

<CheckBox 
    android:id="@+id/ck_t1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0"/> 

<CheckBox 
    android:id="@+id/ck_p1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="25dp" 
    android:layout_weight="1.0" /> 

<CheckBox 
    android:id="@+id/ck_z1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="25dp" 
    android:layout_weight="1.0" /> 
</LinearLayout> 

    <LinearLayout 
    android:id="@+id/LinearLayout05" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" > 

<ImageView 
    android:id="@+id/terranlogo2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/terranlogo" /> 

<ImageView 
    android:id="@+id/protosslogo2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/protosslogo" /> 

<ImageView 
    android:id="@+id/zerglogo2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/zerglogo" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout06" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<CheckBox 
    android:id="@+id/ck_t2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" /> 

<CheckBox 
    android:id="@+id/ck_p2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:layout_marginLeft="25dp" /> 

<CheckBox 
    android:id="@+id/ck_z2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:layout_marginLeft="25dp" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout07" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Choose your opponets race(s) and your race(s)" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout08" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Game Length" 
    android:layout_weight="1.0" 
    android:textSize="15dp" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:text="Mistakes Allowed" 
    android:layout_weight="1.0" 
    android:textSize="15dp" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Answer Time" 
    android:layout_weight="1.0" 
    android:textSize="15dp" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout09" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<Spinner 
    android:id="@+id/s_answertime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
/> 

<Spinner 
    android:id="@+id/s_mistakenumber" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
/> 


<Spinner 
    android:id="@+id/s_gametime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
/> 

</LinearLayout> 
</LinearLayout> 

所有幫助表示讚賞在搞清楚了這一點 - !Lijap

+0

只是一個建議,不要給容器佈局高度0dp,因爲它有孩子在裏面。有它「wrap_content」 – Maxim 2012-07-06 21:55:16

+0

爲什麼權重都相等?你想讓複選框與圖像一樣高?我不明白 - 這不是第一張圖片所顯示的。 – matt5784 2012-07-06 21:58:00

+0

Maxim,在子LinearLayout元素上應該將layout_height設置爲0dp,因爲它避免了第二遍計算佈局高度。這是通過Lint工具推薦的。 – tad 2012-07-06 21:58:11

回答

3

在中間的LinearLayout高度設置爲wrap_content,這是不是你想要的。將根LinearLayout的所有直接子元素設置爲layout_height =「0dp」,您將獲得均勻分佈在高度上的所有項目。

此外,如果您打算讓所有元素佔據容器高度的100%,則weightSum = 7是不需要的。

查看http://ugiagonzalez.com/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/瞭解更多信息。

+1

謝謝!不能相信我沒看到那個wrap_content。 – Lijap 2012-07-06 22:14:18