有三种方法可以使用(提示:使用第3种方法),因为如我在评论中提到的,在Gmail示例中,它不是实际的提示:
Using a Linear Layout, getting a cleaner look in my opinion:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="@string/Hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
</LinearLayout>
Using a Relative Layout, getting a result that mimics the Gmail App:
Note: might be problematic since the text will be displayed on top of the hint, see solution 3.
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="@string/Hint"
android:paddingLeft="10dp"
android:layout_marginBottom="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
Result are as shown in this image:
Edit:
using a Drawable:
This seems a better solution (I personally just created it from snipping the 1:1 display of the TextView, will be in correct measurements this way), this will allow a cleaner layout code, and the text will be aware of the drawable:
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:gravity="top"
android:drawableLeft="@drawable/Hint"
android:layout_height="wrap_content"/>
我脑海中首先想到的是在RelativeLayout中插入两个editText布局。 第一个在第二个上方。在顶部设置背景为 android.R.color.transparent 操作相对布局的顶部元素!在下面的项目中-设置提示(你想要的)并尝试输入文本。可能需要指定顶部第一个元素的填充来获得更好的显示效果。
哦,还有一件事。 也许将editText背景设置为背景图像-但不易于可扩展性。