我有一个带有提示的文本框,但我希望始终显示提示,即使 TB 中有输入也是如此。示例是 Gmail 应用中的“收件人”字段。
最佳答案
您可以使用 3 种方法(剧透 - 使用数字 3),因为正如我的评论中提到的,在 Gmail 示例中,它不是实际的提示:
使用线性布局,我认为外观更清晰:
<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>
使用相对布局,获得模仿 Gmail 应用程序的结果:
注意:可能会出现问题,因为文本将显示在提示上方,请参阅解决方案 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"/>
结果如下图所示:
编辑:
使用 Drawable:
这似乎是一个更好的解决方案(我个人只是通过剪切 TextView 的 1:1 显示来创建它,将以这种方式进行正确的测量),这将允许更清晰的布局代码,并且文本将意识到可绘制:
<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"/>
关于android - 编辑框提示 - 始终显示提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517278/