Android 编辑文本标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3741863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText Label?
提问by Alex Edwards
I want to have some layout a bit like this
我想要一些像这样的布局
[text tabel][edittext]
i cant find an attribute like html esque label. It seems to be the case that i need to use
我找不到像 html esque 标签这样的属性。似乎是我需要使用的情况
[TextView][EditText]
but i cant get them to go on the same line this is my xml file.
但我不能让他们在同一行这是我的 xml 文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/boat_1"/>
<EditText
android:id="@+id/entry"
android:hint="@string/IRC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/boat_2"/>
<EditText
android:id="@+id/entry"
android:hint="@string/IRC"
android:minWidth="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
<Button android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/title_close" />
回答by Cristian
You have basically two options:
您基本上有两种选择:
Option 1: Use nested LinearLayouts:
选项 1:使用嵌套的 LinearLayouts:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/boat_1"/>
<EditText
android:id="@+id/entry"
android:hint="@string/IRC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/boat_2"/>
<EditText
android:id="@+id/entry"
android:hint="@string/IRC"
android:minWidth="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
</LinearLayout>
<Button android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/title_close" />
Notice that I'm using android:orientation="horizontal"
for those nested layouts.
请注意,我正在使用android:orientation="horizontal"
那些嵌套布局。
Option 2: you can use another content manager, like RelativeLayout
.
选项 2:您可以使用其他内容管理器,例如RelativeLayout
.
The advantage of this, is that you can avoid nesting, thus your layout will be easier to read/maintain/inflate. This is a brief example:
这样做的好处是,您可以避免嵌套,因此您的布局将更易于阅读/维护/膨胀。这是一个简单的例子:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_view_boat1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/boat_1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<EditText
android:id="@+id/entry"
android:hint="@string/IRC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_toRightOf="@+id/text_view_boat1"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/text_view_boat2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/boat_2"
android:layout_alignParentLeft="true"
android:layout_below="@id/text_view_boat1"/>
<EditText
android:id="@+id/entry2"
android:hint="@string/IRC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_toRightOf="@+id/text_view_boat2"
android:layout_below="@id/entry"/>
</RelativeLayout>
回答by miguel
The design support library has the TextInputLayout which animates the hint text turning it into a label when the user starts typing and can also show a red error message.
设计支持库具有 TextInputLayout 动画提示文本,当用户开始输入时将其转换为标签,还可以显示红色错误消息。
https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
回答by Mark B
Your LinearLayout has the attribute android:orientation="vertical"
so of course everything is going to be displayed vertically within it.
您的 LinearLayout 具有该属性,android:orientation="vertical"
因此当然所有内容都将在其中垂直显示。
Try wrapping a TextView and EditText with a LinearLayout that has android:orientation="horizontal"
尝试使用具有的 LinearLayout 包装 TextView 和 EditText android:orientation="horizontal"