2013-07-04 42 views
0

所以我在Android的佈局初學者,我正在學習RelativeLayout。我試圖做出如下:Android佈局不如預期

enter image description here

然而,所有我在虛擬設備獲得的名稱是場佔用的空間(寬度和高度)的100%。
enter image description here

這裏是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/root"> 
    <EditText android:id="@+id/name" 
     android:layout_alignParentLeft="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:hint = "Name"/> 
    <EditText android:id="@+id/phone" 
     android:layout_below="@id/name" 
     android:layout_alignParentRight="true" 
     android:layout_weight="0.4" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:hint="Phone" /> 
    <EditText android:id="@+id/email" 
     android:layout_below="@id/phone" 
     android:layout_alignParentRight="true" 
     android:layout_weight="0.4" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:hint="Email" /> 
    <EditText android:id="@+id/dob" 
     android:layout_below="@id/email" 
     android:layout_alignParentRight="true" 
     android:layout_weight="0.4" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:hint="D.O.B." /> 
    <EditText android:id="@+id/address" 
     android:layout_below="@id/phone" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/dob" 
     android:layout_weight="0.6" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:hint="Address" /> 
    <Button android:id="@+id/submit" 
     android:layout_weight="1.0" 
     android:layout_below="@id/address" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" 
     android:text="Submit"/> 
</RelativeLayout> 

到底哪裏出問題了?

回答

2

這是給你輸出的XML。

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

<EditText 
    android:id="@+id/name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:hint="Name" /> 

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

    <EditText 
     android:id="@+id/phone" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true 
     android:layout_weight="-10" 
     android:hint="Phone" /> 

    <EditText 
     android:id="@+id/email" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/phone" 
     android:layout_weight="0.4" 
     android:hint="Email" /> 

    <EditText 
     android:id="@+id/dob" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/email" 
     android:layout_weight="0.4" 
     android:hint="D.O.B." /> 

    <EditText 
     android:id="@+id/address" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/phone" 
     android:layout_toLeftOf="@id/dob" 
     android:layout_weight="0.6" 
     android:hint="Address" /> 
</RelativeLayout> 

<Button 
    android:id="@+id/submit" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"     
    android:text="Submit" /> 

</LinearLayout> 
+0

爲什麼你將它嵌入到'LinearLayout'中? :-) –

+0

用於使'方向'垂直。 'RelativeLayout'不支持'orientation'屬性。正如你所看到的,我只在'RelativeLayout'中添加了中間的'EditText',以便爲你的目的進行設計。頂端的「EditText」和底部的「Button」可以按照方向自動排列。如果您將移動設備的方向從縱向改爲橫向,那麼它也不會中斷。試着用這個xml做一些調整,它會讓你更好的主意。 – Anupam

+0

和'-10'的重量是做什麼的? –

1

更改layout_height的名稱元素: android:layout_height="wrap_content"

+0

什麼錯也'match_parent'嗎? AFAIK,它只意味着「無論父母指派多少」。 –

+0

實際上匹配父母大小。請參閱[developer.android.com](http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT) –

+0

不是一招 - 你應該使用什麼。此外,layout_weights僅適用於線性佈局,因此指定0dp的值不太可能有助於您的佈局。 (如果指定一個固定大小,應該使用dip來獲得設備獨立性) –

1

您的佈局XML更改上面:

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

<EditText 
    android:id="@+id/name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:hint="Name" /> 

<LinearLayout 
    android:id="@+id/linear" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/name" 
    android:orientation="horizontal" 
    android:layout_marginBottom="60dp" 
    android:weightSum="1" > 

    <EditText 
     android:id="@+id/address" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.6" 
     android:hint="Address" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_weight="0.4" 
     android:weightSum="3" > 

     <EditText 
      android:id="@+id/phone" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:hint="Phone" /> 

     <EditText 
      android:id="@+id/email" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:hint="Email" /> 

     <EditText 
      android:id="@+id/dob" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:hint="D.O.B." /> 
    </LinearLayout> 
</LinearLayout> 

<Button 
    android:id="@+id/submit" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:text="Submit" /> 

</RelativeLayout> 

希望幫助:)