2012-11-19 10 views
0

我想了解相對佈局是如何工作的。 我知道如果我使用LinearLayout,我將水平地並排放置兩個編輯文本字段。 使用「重量」將只會根據屏幕大小重新調整edittext字段的大小。 (它可能看起來很奇怪,但它會做到)。Android-Layout:RelativeLayout自動調整edittext字段可能嗎?

我知道linearlayout不是最高效的。我可以使用相對佈局並且具有相同的效果嗎?我不想指定大小,我只想展開edittext。

這樣的xml看起來像這樣的線性佈局嗎?

謝謝 邁克

+2

發佈您的代碼/ XML。 –

+0

讓我困惑的是'可以使用相對佈局並且具有相同的影響?'這是否意味着您還需要一個RelativeLayout代碼? –

回答

0

我想這可能是與alignLeft/alignRight來完成,但遺憾的是它似乎需要一個嵌套的LinearLayout。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" > 

     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="Left" > 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="Right" /> 
    </LinearLayout> 

</RelativeLayout> 
0

如果你只是用WIDTH =「FILL_PARENT」和高度=「FILL_PARENT」中的EditText字段(也許使用一些填充或利潤)?

+0

想到我試了一下,只是把它們堆疊在一起。也許我會困惑一些東西。 – Mike

1

我可以看到大多數android開發者花費在他們的生活上的東西之一是佈局。

看來你可以用RelativeLayouts來做我想要的,但只能用於2的倍數..至少我迄今爲止發現了什麼。使用按鈕中心,兩個按鈕放在同一條線上,每個按鈕佔用大約一半的屏幕。沒有按鈕中心,它們相互重疊。不知道它是否值得。

希望這有助於和謝謝大家!

<View android:id="@+id/button_center" android:layout_width="0dp" 
    android:layout_height="0dp" android:layout_centerHorizontal="true" /> 

<Button android:id="@+id/btnButton1" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="Button 1" 
    android:layout_toLeftOf="@+id/button_center"/> 

<Button android:id="@+id/btnButton2" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="Button 2" 
    android:layout_toRightOf="@+id/button_center" /> 

0

Rememeber是權重不是百分比。重量會根據原始尺寸對物體進行加權。爲了完成你想要的權重只需添加一個android:weightSum到你的LinearLayout。

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1.0" > 

    <EditText 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:text="Left" > 
    </EditText> 

    <EditText 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:text="Right" /> 
</LinearLayout>