2012-06-04 12 views
0

我爲對話框創建了佈局。我希望郵編EditText和Email EditText編輯框的寬度相同,我希望它們左對齊。Android佈局無法使EditText在LinearLayout中排隊

因此,它應該是這樣的:

電子郵件:_ __ _ ___

郵編:_ __ _ ___

更正:其實以上在StackOverflow上顯示不正確。上面的行應該是完全一致的,並且左對齊。所以我在這個論壇上有完全相同的顯示問題。我在電子郵件後放置空格:但它仍然不對齊。

相反,我得到的郵件總是比郵編更廣。我甚至將EditText都改爲相同的輸入類型,但沒有運氣。只有當TextView具有完全相同的文本時,它們纔對齊,如果它們都表示郵政編碼:。如果我使用空格填充電子郵件文本編輯,佈局管理器似乎知道這一點,並將郵件編輯文本展開得比郵政編碼大。非常令人沮喪!

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

<TextView 
    android:id="@+id/messageMsg" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="@dimen/text_21px" 
    android:textColor="@color/white" 
    android:text="lorum ipsum afasdf lajsdfasldfjald:" 
    android:padding="10dip" 
    /> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/emailDescriptor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="@dimen/text_21px" 
     android:textColor="@color/white" 
      android:padding="10dip"    
      android:text="Email: "/> 
    <EditText android:id="@+id/emailId" 
     android:inputType="textEmailAddress" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
      android:layout_margin="10dip"    
      android:textSize="@dimen/text_21px" /> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/zipCodeDescript" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:textSize="@dimen/text_21px" 
     android:text="ZIP Code: " 
     android:textColor="@color/white"/> 
    <EditText android:id="@+id/zCode" 
     android:inputType="textEmailAddress" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:layout_margin="10dip" 
     android:textSize="@dimen/text_21px"/> 
</LinearLayout> 

+0

如果我正確理解你的問題,那你爲什麼不硬編碼TextView的'dp'? – AedonEtLIRA

回答

0

正如@AedonEtLIRA說,你可以硬編碼位置。您可以增加左側的邊距屬性以使其對齊。

或者,你可以使用屬性,

android:layout_alignLeft="@id/your_other_edittext" 

第三個想法,使文本框的標籤相同的「寬度」,那麼下面的edittexts應保持一致。

最後,您可以使用加權巢佈局。但這是一個壞主意。表現不佳&過度混淆。 =)

+0

android:layout_alignLeft對於LinearLayout無效 –

+0

如何讓它們的寬度相同? –

+0

將它們的「寬度」屬性設置爲標籤框的相同編號。你使用4盒還是2盒?我假設4個框:2個文本框標籤和2個edittext輸入框。 – bhekman

0

如果我理解正確的話,試試這個

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Email" /> 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="textEmailAddress" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="7dp" 
      android:text="ZipCode" /> 

     <EditText 
      android:id="@+id/editText2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 
    </TableRow> 

</TableLayout> 
+0

'android:layout_weight =「1」'而不是'android:ems =「10」'將使用左側空格來設置EditText,而不是將其設置爲固定寬度。 – Knickedi

0

今天在一個有用的屬性,迷迷糊糊地想起了這個帖子。

結賬Textview.setEms(int)。 Edittext也會繼承該功能。