繁体   English   中英

在相对布局内将TextView与EditText对齐

[英]Align TextView with EditText inside Relative layout

我在Android画布上看到了一些有关TextViewEditText对齐的帖子。 但是,我没有找到可以以正确的方式帮助我的答案。 我的愿望是以下列方式拥有TextViewEditText

     (TextView) (EditText)          (TextView) (EditText)
     (TextView) (EditText)          (TextView) (EditText)
     (TextView) (EditText)          (TextView) (EditText)
     (TextView) (EditText)

EditText 宽度高度是预定义的(值在dp中给出)。 此外, 文本编辑顶部左侧边距中给出。 这意味着我必须在屏幕上放置一定大小和位置的EditText 没关系,我使用一个RelativeLayout容器实现了它。 但是,我还应该以这种方式放置label( TextView )到EditText旁边,这样我就不会在dps中指定任何值,因为textview必须相对于EditText自动放置。 例如

           Street: [Main Street] 
    Street Number: [     4     ]   

我尝试使用相对布局属性:

  layout_alignTop="@id/EditText"
  layout_toLeftOf="@id/EditText"

但是,未显示TextView,因为它位于EditText左侧,但恰好位于左边缘(顶部对齐有效),而不像我想要的那样“完全”位于EditText旁边(从EditText离开的2dp或3dp)。 我也尝试了其他属性,但没有结果。

  1. 是否有一些原生android属性,可以像我想要的那样将标签放在EditText附近?
  2. 相对布局中是否有一些属性可以为我工作?
  3. 有没有办法只指定TextViewEditText还剩3 dp?
  4. 还有其他解决方案吗?

谢谢

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

    <EditText
    android:id="@+id/EditText1"
    android:hint="Write..."
    android:layout_width="80dp"
    android:layout_height="22dp"
    android:layout_marginTop="61dp"
    android:layout_marginLeft="160dp"
    android:background="@android:drawable/editbox_dropdown_light_frame"/>

    <TextView
    android:id="@+id/TextView1"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:text="Text"
    android:layout_toLeftOf="@+id/EditText1"
    android:layout_alignTop="@+id/EditText1"/>

    <EditText
    android:id="@+id/EditText2"
    android:hint="Write..."
    android:layout_width="200dp"
    android:layout_height="22dp"
    android:layout_marginTop="61dp"
    android:layout_marginLeft="490dp"
    android:background="@android:drawable/editbox_dropdown_light_frame"/>

    <TextView
    android:id="@+id/TextView2"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:text="Text"
    android:layout_alignTop="@+id/EditText2"
    android:layout_toLeftOf="@+id/EditText2" />

    </RelativeLayout>

您正在使用左layout_margin来创建问题,如果只需要这么多,为什么不使用TableLayout

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow
        android:layout_marginTop="61dp">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">
        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="22dp"
            android:text="Text" />
        <EditText
            android:id="@+id/EditText1"
            android:hint="Write..."
            android:layout_width="80dp"
            android:layout_height="22dp"
            android:background="@android:drawable/editbox_dropdown_light_frame"/>


    </LinearLayout>


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1"
        >
        <TextView
            android:id="@+id/TextView2"
            android:layout_width="wrap_content"
            android:layout_height="22dp"
            android:text="Text" />
        <EditText
            android:id="@+id/EditText2"
            android:hint="Write..."
            android:layout_width="80dp"
            android:layout_height="22dp"
            android:background="@android:drawable/editbox_dropdown_light_frame"/>


    </LinearLayout>
</TableRow>
</TableLayout>

只需复制整个TableRow标签并将其粘贴到所需的任意行即可

**但是如果您仍然想要相对布局,这是您的固定代码**只需根据需要正确更改边距值

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

<TextView
    android:id="@+id/TextView1"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:text="Text"
    android:layout_marginTop="57dp"
    android:layout_marginLeft="80dp"
    />

<EditText
    android:id="@+id/EditText1"
    android:hint="Write..."
    android:layout_width="80dp"
    android:layout_height="22dp"
    android:background="@android:drawable/editbox_dropdown_light_frame"
    android:layout_alignTop="@+id/TextView1"
    android:layout_toRightOf="@+id/TextView1"
    android:layout_toEndOf="@+id/TextView1" />



<EditText
    android:id="@+id/EditText2"
    android:hint="Write..."
    android:layout_width="80dp"
    android:layout_height="22dp"
    android:background="@android:drawable/editbox_dropdown_light_frame"
    android:layout_alignTop="@+id/EditText1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="25dp"
    android:layout_marginEnd="25dp" />

<TextView
    android:id="@+id/TextView2"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:text="Text"
    android:layout_alignTop="@+id/EditText2"
    android:layout_toLeftOf="@+id/EditText2"
    android:layout_toStartOf="@+id/EditText2" />

只需排列XML,就可以使用padding实现所需的功能。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingRight="16dp">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM