如何设置Android中TextInputLayout分割线与错误文本的间距?
如何调整TextInputLayout分割线与错误文本的间距
嘿,我懂你碰到的这个问题——之前我也试过在errorTextAppearance指定的样式里加内边距,结果完全没效果,后来才搞明白原因:这个样式里的padding是作用于错误文本自身的,没法改变它和上方分割线的间距。给你两个靠谱的解决办法:
方法一:直接用TextInputLayout的专属属性
Google给TextInputLayout提供了专门控制错误文本间距的属性app:errorTextMarginTop,直接在布局里给TextInputLayout加上这个属性就能搞定,比如设置16dp的间距:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="test.textinputlayouttest.MainActivity"> <android.support.design.widget.TextInputLayout android:id="@+id/address" android:layout_width="match_parent" android:layout_height="wrap_content" app:errorEnabled="true" app:errorTextAppearance="@style/Error" app:errorTextMarginTop="16dp"> <!-- 新增这个属性控制间距 --> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter address" android:inputType="textEmailAddress" android:padding="26dp"/> </android.support.design.widget.TextInputLayout> </android.support.constraint.ConstraintLayout>
方法二:在错误文本样式里设置外边距
如果你想在样式里统一控制所有错误文本的间距,可以把原来样式里的android:paddingBottom改成android:marginTop,因为外边距才是控制元素和其他组件之间的距离:
<style name="Error" parent="TextAppearance.AppCompat"> <item name="android:textColor">@color/red</item> <item name="android:marginTop">16dp</item> <!-- 用marginTop替代paddingBottom --> </style>
这两种方法都能有效拉开错误文本和分割线的距离,你可以根据自己的需求选一种就行~
内容的提问来源于stack exchange,提问作者buellas
相关产品推荐
相关产品推荐

