2012-07-13 90 views
0

下面是我用於創建帶有標題「在此輸入電話號碼」的文本框的xml代碼。我有一個編輯框可以獲取電話號碼,也可以獲得一個按鈕。 問題是文本框和編輯框相互重疊。如何解決這個問題呢。定義佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView android:id="@+id/textLabel" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="Enter number to dial" 
     /> 
    <EditText android:id="@+id/phoneNumber" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     /> 
    <Button android:id="@+id/callButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="Show Dialer" 
    /> 


</RelativeLayout> 
+0

如果您的應用的目標API級別爲8+,則應始終使用match_parent而不是fill_parent。 Fill_parent在API級別8中重命名爲match_parent。認爲它們在功能上相同,match_parent現在是標準。 – 2012-07-13 17:04:25

回答

0

您應該瞭解更多關於各種LayoutParams of RelativeLayout。 下面的代碼使用了其中的一些,這也避免了重疊問題。

<TextView android:id="@+id/textLabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:text="Enter number to dial" 
    /> 
<EditText android:id="@+id/phoneNumber" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/textLabel" 
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="8dp" 
    /> 
<Button android:id="@+id/callButton" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textLabel" 
    android:layout_marginTop="8dp" 
    android:text="Show Dialer" 
    /> 
0

使用LinearLayout而不是RelativeLayout來定位水平或垂直。 LinearLayout以線性方式自動排列內容。

1

如果您正在使用Eclispe進行開發,只需切換到圖形佈局選項,您可以在其中拖動項目並定位它們[這會將上述答案中的代碼添加到XML中]。檢查this視頻以瞭解RelativeLayout以及如何使用它。 [視頻來源:Google I/O 2012]

1

如果您希望從另一個項目跨越某個項目,最好使用TableLayout。我確信其他人有一個最喜歡的方式,但這是我的。

<?xml version="1.0" encoding="utf-8"?> 
    <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:background="#000044"> 
     <TableRow> 
      <TextView 
       android:text="Name:" 
       android:width ="120px" 
       /> 
      <EditText 
       android:id="@+id/someUserName" 
       android:width="200px" /> 
     </TableRow> 
     <TableRow> 
      <TextView 
       android:text="Phone:" 
       /> 
      <EditText 
       android:id="@+id/somePhone" 
       /> 
     </TableRow> 
     <TableRow> 
      <TextView 
       android:text="Address:" 
       /> 
      <EditText 
       android:id="@+id/someAddress" 
       /> 
     </TableRow> 
     <TableRow> 
      <Button 
       android:id="@+id/buttonToClick" 
       android:text="Go Do Something Cool" /> 
     </TableRow> 
    </TableLayout> 

如果你想在一排三個項目做到這一點

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

    <TableRow> 
     <TextView 
      android:text="Call Number" 
      android:paddingRight="50px" 
      /> 
     <EditText 
      android:id="@+id/someNumber" 
      android:width ="120px" 
      android:paddingLeft="10px" 
      /> 

     <Button 
      android:id="@+id/buttonToClick" 
      android:text="Call" /> 

    </TableRow> 
</TableLayout> 
0

使用Android:layout_below = 「@ + ID /爲textLabel」 屬性,在的EditText如下:

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

    <TextView 
     android:id="@+id/textLabel" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="Enter number to dial" /> 

    <EditText 
     android:id="@+id/phoneNumber" 
     android:layout_below="@+id/textLabel" // <--- MUST Add This 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="Now phoneNumber Will Appear" // <--- See This 
     android:layout_alignParentRight="true" /> 

    <Button 
     android:id="@+id/callButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Show Dialer" /> 

</RelativeLayout>