将文本视图垂直居中于 EditText
我想做的是有一个 TextView,在它的右侧有一个 editText 占据剩余的空间。 textView 应沿 editText 垂直居中。现在我可以使用线性布局来做到这一点,但我更喜欢在相对布局中做到这一点。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/loginText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login:"
android:paddingRight="20dp" />
<EditText
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/loginText" />
</RelativeLayout>
What I'm trying to do is have a TextView, and to the right of it an editText that takes up the rest of the space. The textView should be centered vertically along the editText. Now I can do this using an linearlayout, however I would prefer to do it in a relativelayout.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/loginText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login:"
android:paddingRight="20dp" />
<EditText
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/loginText" />
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要做的事情可以通过使用
android:layout_alignBottom
和 < code>android:layout_alignTop 由RelativeLayout
为其子级和TextView
的android:gravity
属性。这些属性允许您设置视图的底部/顶部边缘以匹配另一个视图的底部/顶部边缘。通过将它们应用到
TextView
,您可以有效地使TextView
与EditText
控件的高度相匹配。然后,使用重力属性,您可以使TextView
中的文本在TextView
控件内垂直居中。以下示例应该按您的预期工作:
What you want to do is possible by using the
android:layout_alignBottom
andandroid:layout_alignTop
properties provided by theRelativeLayout
to its children and theandroid:gravity
property of theTextView
.These properties allow you to set the bottom/top edge of a view to match the bottom/top edge of another view. By applying them to the
TextView
you can effectively make theTextView
match the height of theEditText
control. Then, with the gravity property you can make the text in theTextView
center vertically inside theTextView
control.The following example should work as you intended:
将
android:layout_gravity="center_vertical"
添加到您的 Textview这会将 textview 垂直居中放置在父级中
add
android:layout_gravity="center_vertical"
to your Textviewthis would place the textview centered vertically in the parent