TextInputLayout :如何提供填充或边距来提示? [英] TextInputLayout :How to give padding or margin to hint?
问题描述
我必须在我的项目中使用设计支持库的 TextInputLayout
.我想在 TextInputLayout
中的 hint
和 EditText
之间留出空间.我在 TextInputLayout
甚至在 EditText
中设置了边距和填充,但两者都不起作用.那么如何解决这个问题.在这里我附上屏幕截图和我的编码.
I have to use TextInputLayout
of design support library in my project. I want to give space between hint
and EditText
in TextInputLayout
. I set margin and padding in TextInputLayout
and even inside EditText
but both are not work.So how to solve this issue. Here i attach screen shot and my coding.
==============================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>
推荐答案
ganesh2shiv 提出的解决方案在大多数情况下都有效,尽管我发现它也可以在未聚焦时使 EditText 内显示的提示文本分散.
The solution proposed by ganesh2shiv works for the most part, although I've found it also de-centres the hint text displayed inside the EditText when not focused.
更好的技巧是将所需的 paddingTop
设置为 EditText,同时将额外的内边距嵌入到 EditText 的背景中.一种相当明智的方法是将原始背景包装在
中并设置
code> 属性以匹配 EditText 的 paddingTop
.
A better trick is to set the desired paddingTop
to the EditText but also embed the extra padding within the EditText's background. A fairly sane way to do this is to wrap your original background in a <layer-list>
and set the <item android:top="...">
attribute to match the paddingTop
of your 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>
这篇关于TextInputLayout :如何提供填充或边距来提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!