2012-09-10 47 views
3

我很難實現,兩個部件(稱爲微調器)以線性佈局相鄰。我的意思是這兩個旋轉器的佈局高度都是包裝內容,但寬度應該是第一個旋轉器的前半部分,第二個旋轉器的後半部分。在線性佈局中,他們一個接一個。我嘗試了相對佈局,但是因爲我給出了寬度,因爲wrap_content都是相鄰的,但很多空間仍然是第二個微調。我在幾個應用程序中看到了這個問題,但我沒有得到它。線性佈局中的兩個部件相鄰其他部件

enter image description here

回答

8

使用layout_weight。這將迫使兩個紡紗者分別佔據一半的空間。

<LinearLayout 
    android:orientation="horizontal" 
    ... > 

    <Spinner 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     ... /> 

    <Spinner 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     ... /> 

</LinearLayout> 
+0

我認爲這將使高度一半每個嗎? – Vins

+1

不是。這是一個「水平」佈局,所以重量將水平應用。 – Eric

+0

在我的佈局中,我有一個更像上圖中的微調,所以我使用了一個更多的線性佈局,並將其設置爲水平方向。我會張貼這個答案,但這是靈感來自你的答案,所以我接受你的:) – Vins

0

我有完全相同的問題一天。我也嘗試了很多不同的技巧來讓它工作。我結束了兩個紡紗廠的佈局。很奇怪,但它的工作。

+0

我想我可以提明確的size..but做那不是正確的方法去實現它 – Vins

0

相對佈局旨在提供彼此相關的視圖。它與它們的尺寸無關。

對於linearLayout,將兩個視圖的高度/寬度(取決於佈局的方向)設置爲0px,並將權重設置爲1。這將使每個人都佔據一半的空間。

0
<?xml version="1.0" encoding="utf-8"?> 

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:entries="@array/testArray" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/background_light" 
    android:orientation="horizontal" > 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:entries="@array/testArray" /> 

    <Spinner 
     android:id="@+id/spinner3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout>  
</LinearLayout> 
相關問題