Android RelativeLayout - 如何将两个元素放在同一行?

标签 android android-layout android-relativelayout

问题是:我有一个 TextView 和一个 EditText,如何将它们放在同一“层级”?

http://postimage.org/image/o9l17a3zv/

如您所见,我想将“用户名”放在与 EditText 相同的级别...

这是我的代码:

<TextView
    android:id="@+id/account_creation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/account_creation"
    android:textSize="20sp"
    android:textStyle="bold" />

<!-- Username -->

<TextView
    android:id="@+id/username_registration"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/account_creation"
    android:layout_toLeftOf="@+id/username_new_account"
    android:text="@string/user_text"
    android:textSize="15sp" />

<EditText
    android:id="@+id/username_new_account"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/account_creation"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:hint="@string/user_editext" >

    <requestFocus />
</EditText>

我想给他们两个设置相同的“高度”值,但我不知道这是否是正确/更好的方法...

最佳答案

文本查看和编辑文本在同一条线上,触及父布局的底线 试试这个。

android:layout_alignBottom="@+id/editText1"

这一定会对你有所帮助。

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

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

谢谢。

关于Android RelativeLayout - 如何将两个元素放在同一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11577570/

相关文章:

android - 使用 XML 布局作为 View 子类的 View ?

android - 我在尝试运行 Android 虚拟设备时收到错误列表

android - LinearLayout 使用 drawableLeft 将文本对齐到中心 View

android - setLayoutParams 弄乱了 RelativeLayout

android - 将按钮与另一个布局中的图像对齐

android - 动态添加 View 到 RelativeLayout 的底部

android - 错误 :Lambda coming from jar file need their interfaces on the classpath to be compiled RxJava2

android - doInBackground 运行时是否可以停止 asynctask?

android - 将 EditText 与按钮对齐

android - 在选择 EditText 之前禁用 Android 中的软件键盘