2012-05-05 27 views
1

在Android中,我試圖做到這一點: Search EditText and Button side by side如何把EditView和Button並排佔據90%的寬度?

基本上,按鈕並排着一個EditText,他們都接受了90%的頂行的寬度增加的能力。 (我不關心像twitter圖標那樣的標誌)。

我試過的LinearLayout與layout_weight,但他們並沒有出現在正確的所有:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum = "1.0" 
    android:orientation="vertical" 
    android:background = "#faadadad" > 

    <EditText 
     android:layout_width="0dip" 
     android:layout_height="0dip" 
     android:layout_weight = "0.8" 
     android:layout_marginLeft = "2dip" 
     android:layout_marginTop = "1dip" 
     android:layout_marginBottom = "1dip"   
     android:hint="Search Terms" /> 

    <Button android:text = "?" 
     android:layout_width = "0dip" 
     android:layout_height="2dip" android:layout_weight = "0.2"/> 

    </LinearLayout>  

,任何事情我在RelativeLayout的嘗試只是不看的權利(我嘗試設置2之間的空白元素爲0dips,沒有運氣,我也無法獲得90%的寬度要求。)

我在做什麼錯?提前致謝。

+0

可能dublicate的http://stackoverflow.com/questions/2950885/combining-edittext-and-button – superM

回答

2

嘗試使用以下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum = "1.0" 
    android:orientation="horizontal" 
    android:background = "#faadadad" > 
    <LinearLayout android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.8">  
     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft = "2dip" 
      android:layout_marginTop = "1dip" 
      android:layout_marginBottom = "1dip"   
      android:hint="Search Terms" /> 
    </LinearLayout> 
    <Button android:text = "?" 
     android:layout_width = "0dip" 
     android:layout_height="wrap_content" 
    android:layout_weight = "0.2"/> 
</LinearLayout> 

因此,基本上,在使用的,水平的LinearLayout你,然後layout_weight水平工作即寬度根據layout_weight參數(layout_width = 「0dp」)劃分。如果您使用垂直LinearLayout,則layout_weight會垂直工作,即根據layout_weight參數(layout_height =「0dp」)垂直分割高度。

希望它對你有幫助。

+0

幾乎完美。然而,EditView和Button之間有一個間隙按鈕,並且不會在它們之間留出餘量而無法修復它。 – StackOverflowed

+0

只需將EditText包裝成某種佈局即可。更新XML ..檢查出.. – Veer

相關問題