android - 编辑框提示 - 始终显示提示

标签 android xml layout

我有一个带有提示的文本框,但我希望始终显示提示,即使 TB 中有输入也是如此。示例是 Gmail 应用中的“收件人”字段。

最佳答案

您可以使用 3 种方法(剧透 - 使用数字 3),因为正如我的评论中提到的,在 Gmail 示例中,它不是实际的提示:

  1. 使用线性布局,我认为外观更清晰:

    <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>
    
  2. 使用相对布局,获得模仿 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"/>    
    

    结果如下图所示:

    Custom Permanent Hint

    编辑:

  3. 使用 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"/>    
    

    Drawable Hint

关于android - 编辑框提示 - 始终显示提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517278/

相关文章:

javascript - Javascript 中的 XXE 漏洞

java - Retrofit 中 RSS 提要解析期间的 ValueRequiredException

iphone - 为什么我的 UIActivityIndi​​cator 在 UIButton 中作为 subview 进入负 x 坐标

c++ - 从代码访问在 UI 中创建的 Qt 布局?

css - 使用 CSS 强制 float column-1 中的超宽元素出现在 float column-2 下方

android - java.lang.RuntimeException : Error receiving broadcast Intent On install Apk 错误

java - 如何从android中的.txt文件中获取字符串

android - 如何管理 Android Webview 的空白加载屏幕?

.net - 使用 XmlSerializer 读取 UTF-16 编码的 XML 文件

android - 阅读PDF文件