2010-08-18 105 views
8

我在我的LinearLayout中有一個EditText和一個Button,我想將它們緊密排列在一起,讓它們看起來似乎屬於一起(edittext + micButton用於語音輸入)。 現在它們沒有相同的高度,它們並沒有很好地對齊(按鈕看起來比EditText略低)。我知道我可以使用像-5dip這樣的負邊距來讓它們靠得更近,但是有沒有更好的方法來做到這一點?將它們設置在特定的容器/佈局中,以便它們自動具有相同的高度並且它們之間沒有空白。在Android中製作EditText和Button相同的高度

回答

-1

Android會嘗試自動將所有內容放在文本之外,而不是按鈕本身。

帶我永遠終於搞清楚了。它非常簡單。應該修復它。

myButton.setGravity(Gravity.CENTER_VERTICAL); 

或者如果它們在一行中,則將按鈕附加到表格行。

myTableRow.setGravity(Gravity.CENTER_VERTICAL); 
0

使用相對佈局就可以拉伸而不知道其它視圖的確切大小取決於另一視圖的大小的圖。 下面是代碼:

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
<Button 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:text="button" 
    android:id="@+id/but"/> 
<EditText 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/but" 
    android:layout_alignBottom="@id/but" 
    android:layout_alignTop="@id/but" 
    /> 
</RelativeLayout> 

檢查此鏈接視圖之間減少空間: https://groups.google.com/forum/?fromgroups#!topic/android-developers/RNfAxbqbTIk

0

嗯,不知道爲什麼人不屑這麼多表。

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

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" /> 

    </LinearLayout> 

注意:由於這兩種觀點都是LinearLayout中(推測方向=水平)內,此命令應該佈局內中心都因爲EditTexts和按鈕可以定位自己的文字略有不同,您可能需要做一些調整(通過更改邊距或填充)以使文本正確對齊。

0

我希望這個解決方案可以幫助您的方案...這裏是代碼..

<RelativeLayout 
     android:id="@+id/rlLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:orientation="horizontal" 
     android:padding="3dp" > 

    <EditText 
     android:id="@+id/etId" 
     android:layout_width="fill_parent" 
     android:layout_height="50dip" 
     android:background="#c8c8c8" 
     android:hint="Edittext" 
     android:paddingLeft="20dip" 
     android:paddingRight="10dip" 
     android:textColor="#000000" /> 

    <RelativeLayout 
     android:id="@+id/rlLayoutid" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignRight="@+id/etId" > 

     <Button 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="14dp" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/calender" /> 
    </RelativeLayout> 
</RelativeLayout> 

0

@丹尼爾在這裏你可以使用佈局以及重總和

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center" 
     android:weight_sum=2  
     > 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=1 
     android:text="button" 
     android:id="@+id/but"/> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=1  
     /> 
    </LinearLayout>