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>
4

5 回答 5

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>
于 2012-07-13T17:31:27.170 回答
1

如果您使用 Eclispe 进行开发,只需切换到图形布局选项,您可以在其中拖动项目并定位它们[这会将上述答案中的代码添加到您的 XML]。查看视频以RelativeLayout了解它以及如何使用它。[视频来源:谷歌 I/O 2012]

于 2012-07-13T17:17:27.043 回答
0

在 EditText 中使用 android:layout_below="@+id/textLabel" 属性,如下所示:

<?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>
于 2012-07-13T17:36:08.317 回答
0

您应该了解有关RelativeLayout 的各种 LayoutParams 的更多信息。下面的代码使用了其中的一些,这也避免了重叠问题。

<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"
     />
于 2012-07-13T17:02:37.507 回答
0

使用 LinearLayout 而不是具有水平或垂直方向的 RelativeLayout。LinearLayout 自动以线性方式排列内容。

于 2012-07-13T17:04:14.123 回答