在相对布局中将 TextView 与 EditText 对齐
Align TextView with EditText inside Relative layout
我在 Android canvas 上看到一些关于 TextView
与 EditText
对齐的帖子。但是,我没有找到可以以正确方式帮助我的答案。我的愿望是以下列方式获得 TextView
和 EditText
:
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText)
EditText
width 和 height 是预定义的(值以 dp 给出)。此外,还给出了 TextEdit 的 top 和 left margin。这意味着我必须将 EditText
放置在屏幕上的特定大小和特定位置。这没关系,我使用一个 RelativeLayout
容器实现了它。
但我还应该将 label(TextView
) 放在 EditText
旁边,这样我就不会在 dps 中指定任何值,因为文本视图必须根据 EditText
自动放置。例如
Street: [Main Street]
Street Number: [ 4 ]
我尝试使用相对布局属性:
layout_alignTop="@id/EditText"
layout_toLeftOf="@id/EditText"
但是,TextView 没有显示,因为它位于 EditText
的左侧,但恰好位于左边距(顶部对齐有效)而不是 "exactly" 旁边的 EditText(距 [ 2dp 或 3dp) =15=]) 就像我想要的那样。
我也尝试了其他属性但没有结果。
- 是否有一些本机 android 属性可以像我想要的那样将标签放在
EditText
附近?
- 相对布局中是否有一些可以为我工作的属性?
- 有没有办法只指定
TextView
是 EditText
的 3 dp?
- 还有其他解决方案吗?
谢谢
<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 left,这会造成问题,如果您的需求就这么多,为什么不使用 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,你就可以使用填充来实现你想要的。
<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">
我在 Android canvas 上看到一些关于 TextView
与 EditText
对齐的帖子。但是,我没有找到可以以正确方式帮助我的答案。我的愿望是以下列方式获得 TextView
和 EditText
:
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText) (TextView) (EditText)
(TextView) (EditText)
EditText
width 和 height 是预定义的(值以 dp 给出)。此外,还给出了 TextEdit 的 top 和 left margin。这意味着我必须将 EditText
放置在屏幕上的特定大小和特定位置。这没关系,我使用一个 RelativeLayout
容器实现了它。
但我还应该将 label(TextView
) 放在 EditText
旁边,这样我就不会在 dps 中指定任何值,因为文本视图必须根据 EditText
自动放置。例如
Street: [Main Street]
Street Number: [ 4 ]
我尝试使用相对布局属性:
layout_alignTop="@id/EditText"
layout_toLeftOf="@id/EditText"
但是,TextView 没有显示,因为它位于 EditText
的左侧,但恰好位于左边距(顶部对齐有效)而不是 "exactly" 旁边的 EditText(距 [ 2dp 或 3dp) =15=]) 就像我想要的那样。
我也尝试了其他属性但没有结果。
- 是否有一些本机 android 属性可以像我想要的那样将标签放在
EditText
附近? - 相对布局中是否有一些可以为我工作的属性?
- 有没有办法只指定
TextView
是EditText
的 3 dp? - 还有其他解决方案吗?
谢谢
<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 left,这会造成问题,如果您的需求就这么多,为什么不使用 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,你就可以使用填充来实现你想要的。
<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">