TextInputLayout:如何给提示填充或边距?

新手上路,请多包涵

我必须在我的项目中使用设计支持库的 TextInputLayout 。我想在 --- 中的 TextInputLayout hintEditText 之间留出空间。我在 TextInputLayout 甚至在 EditText 中设置了边距和填充,但两者都不起作用。那么如何解决这个问题。在这里我附上屏幕截图和我的编码。

 ==============================Style=================================

<style name="TextHint" parent="Base.TextAppearance.AppCompat">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/green</item>
</style>

=============================XML===================================
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    app:hintTextAppearance="@style/TextHint"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_height="wrap_content">
<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/edttxtEmailAddress"
    android:singleLine="true"
    android:hint="@string/enter_valid_email"
    android:paddingLeft="20dp"
    android:textSize="20sp"
    android:background="@drawable/rounded_common"/>
</android.support.design.widget.TextInputLayout>

在此处输入图像描述

原文由 Pranav 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 787
2 个回答

ganesh2shiv 提出的解决方案在大多数情况下都有效,尽管我发现它也会在未聚焦时使 EditText 内显示的提示文本偏离中心。

一个更好的技巧是将所需的 paddingTop 设置为 EditText,但还要在 EditText 的背景中嵌入额外的填充。一种相当明智的方法是将原始背景包装在 <layer-list> 中,并设置 <item android:top="..."> 属性以匹配 paddingTop 的 EditText.—

 <android.support.design.widget.TextInputLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/floating_hint_margin"
    android:background="@drawable/bg_edit_text" />
</android.support.design.widget.TextInputLayout>

bg_edit_text.xml 可绘制文件:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:top="@dimen/floating_hint_margin">
    <your original background; can be <bitmap> or <shape> or whatever./>
  </item>
</layer-list>

原文由 Radu Topor 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您想将 android:hint 填充设置为 0 焦点,请将 app:boxCollapsedPaddingTop="0dp" 添加到您的 TextInputLayout

原文由 Erkan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题