2011-07-05 46 views
1

我有一個用行填充的ListView。這些行來自看起來一個XML文件,如:LinearLayout layout_weight

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="8dip" 
android:weightSum="100"> 
<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/columnA" 
    android:layout_weight="30" 
    android:layout_gravity="center"/> 
<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/columnB" 
    android:layout_weight="30" 
    android:layout_gravity="center"/> 
<ImageView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:src="@drawable/icon" 
    android:id="@+id/columnC" 
    android:layout_weight="10" 
    android:layout_gravity="center" 
    > 
</ImageView>  
<CheckBox 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/columnD" 
    android:layout_weight="30" 
    android:layout_gravity="center"/> 

的問題是,我想columnC項目和columnB項目非常接近在一起,讓我排在三個等距間隔出部分,即:columnA,(columnB + columnC),然後是columnD。我試圖通過使用layout_weight來實現此目的,但是上面的代碼似乎有相反的效果。柱A和柱B在左邊非常擠壓,柱C似乎在它自己的大空間中漂浮,然後柱D位於靠近柱C,其右側的空間太多。我究竟做錯了什麼? :s

回答

0

我認爲你希望columnC和columnB具有相同的權重,但是你將它設置爲與他們不同。

嘗試

columnA:權重= 2

columnB:權重= 1

columnC:權重= 1

columnD:權重= 2

我具有非常有限的經驗與重量屬性,但我認爲這是如何得到你想要的結果。

如果您仍然遇到問題,可以幫助我們幫助您,如果您可以發佈屏幕截圖,瞭解它的外觀以及您希望如何顯示。

+0

打我吧,沒有意識到多少......沒有得到「加載1個新答案」,直到一會兒在我發佈之前。 – Maximus

+0

感謝你們倆,這絕對看起來更好,但是columnD仍然顯得太靠近columnC了,右邊有太多空間:s可能與其複選框有關。 –

+0

好吧 - 事實證明,我不得不把我的複選框包裹在一個線性佈局中以解決這個剩餘的問題,然後在線性佈局中設置重量和重力。謝謝! –

2

試試看。基本上可以計算每個視圖按重量/總重量「需要」多少空間。你不必試圖讓它們等於100.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="8dip" 
    android:weightSum="100"> 
<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/columnA" 
    android:layout_weight="2" 
    android:layout_gravity="center"/> 
<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/columnB" 
    android:layout_weight="1" 
    android:layout_gravity="center"/> 
<ImageView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:src="@drawable/icon" 
    android:id="@+id/columnC" 
    android:layout_weight="1" 
    android:layout_gravity="center" 
    > 
</ImageView>  
<CheckBox 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/columnD" 
    android:layout_weight="2" 
    android:layout_gravity="center"/>