Android:摆脱EditText底部的额外空间

时间:2016-10-27 11:02:52

标签: android xml android-edittext android-textinputlayout

我正在使用放置在TextInputLayouts中的EditTexts来实现一些输入字段。我的问题是EditTexts底部似乎有一些额外的空间,当我将几个输入字段放在彼此之下时这是显而易见的。

这是我看到的图片,输入字段之间的间隙可见(以红色框起)。

enter image description here

我想摆脱这个额外的空间。将填充和边距设置为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>

2 个答案:

答案 0 :(得分:0)

试试这个

 android:layout_marginTop="-10dp"  

对于您的第二个编辑文字,请根据您的方便使用该值

答案 1 :(得分:0)

实际上没有属性可以删除该空间,但是您可以通过使用自定义背景来编辑文本来删除该空间。对于自定义背景,请点击here

相关问题