【发布时间】:2016-10-27 11:02:52
【问题描述】:
我正在使用放置在 TextInputLayouts 中的 EditTexts 来实现一些输入字段。我的问题是 EditTexts 的底部似乎有一些额外的空间,当我将几个输入字段放在一起时,这一点很明显。
这是我看到的图片,输入字段之间的间隙可见(用红色框起来)。
我想摆脱这个多余的空间。将填充和边距设置为 0 并没有帮助,虽然我知道我可以尝试应用负边距,但我真的很想避免这种解决方案,因为它会导致 UI 测试出现一些问题。有没有人知道如何消除多余的空间?
一些代码:
EditText 和 TextInputLayout 的样式(注意默认的边距尺寸值都是 0dp):
<style name="EditTextLayout">
<item name="android:layout_height">@dimen/input_field_height</item>
<item name="android:minHeight">@dimen/input_field_height</item>
<item name="android:background">@drawable/input_field_background</item>
<item name="android:paddingBottom">@dimen/default_margin_bottom</item>
<item name="android:paddingStart">@dimen/default_margin_left</item>
<item name="android:paddingEnd">@dimen/default_margin_right</item>
<item name="android:paddingTop">@dimen/default_padding_top</item>
<item name="android:layout_marginBottom">@dimen/default_margin_bottom</item>
<item name="android:textColorHint">@color/input_field_floating_label</item>
</style>
<style name="EditTextTheme">
<item name="android:imeOptions">actionDone</item>
<item name="android:maxLines">1</item>
<item name="colorControlNormal">@color/primary_line_color</item>
<item name="colorControlActivated">@color/nordea_blue</item>
<item name="colorControlHighlight">@color/error_color</item>
<item name="android:textColorPrimary">@color/input_field_text</item>
<item name="android:textSize">@dimen/textsize_caption</item>
<item name="android:textColorHint">@color/input_field_floating_label</item>
</style>
<style name="EditText">
<item name="android:theme">@style/EditTextTheme</item>
<item name="android:textCursorDrawable">@drawable/cursor_blue</item>
<item name="android:paddingStart">@dimen/default_padding</item>
<item name="android:paddingEnd">@dimen/default_padding</item>
<item name="android:paddingTop">@dimen/input_field_floating_label_padding</item>
<item name="android:layout_marginBottom">@dimen/default_margin_bottom</item>
<item name="android:background">@drawable/input_field_divider</item>
</style>
EditText 的背景(只是为了让底线变粗):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
</shape>
</item>
<item
android:left="@dimen/input_field_background_side_margin"
android:right="@dimen/input_field_background_side_margin"
android:top="@dimen/input_field_background_side_margin">
<shape>
<solid android:color="@android:color/transparent"/>
<stroke
android:width="@dimen/input_field_divider_height"
android:color="@color/input_field_divider_color"></stroke>
<padding
android:bottom="@dimen/testlogin_dropdown_divider_padding"
android:top="@dimen/input_field_floating_label_padding"/>
</shape>
</item>
</layer-list>
TextInputLayout 的背景:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="@dimen/inputfield_background_light_margin_bottom">
<shape android:shape="rectangle">
<solid android:color="@color/input_field_layout_bg"/>
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp" />
</shape>
</item>
</layer-list>
用法:
<android.support.design.widget.TextInputLayout
android:id="@+id/input_container"
style="@style/EditTextLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/input_field_height"
app:errorTextAppearance="@style/EditTextErrorText"
app:hintTextAppearance="@style/EditTextFloatingLabel"
tools:hint="Hint text"
tools:text="Label text">
<android.support.design.widget.TextInputEditText
android:id="@+id/input_editfield"
style="@style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="text|textCapSentences"/>
</android.support.design.widget.TextInputLayout>
【问题讨论】:
-
你试过完全删除
padding和marginBottom吗?还有,这么多0dppaddings有什么意义? -
我不确定你能否尝试更改 testlogin_dropdown_divider_padding 和 input_field_floating_label_padding 的值作为测试用例?
-
@Prad,我尝试了所有这些,删除了填充和边距,更改了值等。然而,无论我做什么,底部总是有额外的空间。即使我尝试将填充和边距设置为 0,它仍然会出现。我想知道是否有人知道该空间的确切来源以及我能做些什么来摆脱它。正如我之前所说,负利润率对我来说不是好的解决方案,所以我正在寻找替代方案。
标签: android xml android-edittext android-textinputlayout