本文介绍了AppCompat Spinner的TextInputLayout错误对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个显示下拉菜单Spinner且旁边是EditText作为其子元素TextInputLayout的应用程序.

I am using an app which displays a drop down Spinner with an EditText as a child of TextInputLayout next to it.

TextInputLayout下显示带下划线的错误时,EditText将上升,并且对齐方式将丢失!

When an underlined error is shown under the TextInputLayout, then the EditText will go up, and alignment will miss !!

请查看我的屏幕截图:

我想做的是:

这是我的xml代码:

<LinearLayout
    android:layout_width="@dimen/edittext_width"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="bottom">

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/spinner_countries"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:layout_gravity="start"
        android:spinnerMode="dialog" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_mobile"
        style="@style/TextInputLayoutTheme"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="70">

        <com.cashu.app.ui.widgets.CustomEditText
            android:id="@+id/et_fragment_register_step_one_mobile"
            android:layout_width="@dimen/edittext_mobile_num_width"
            android:layout_height="wrap_content"
            android:ems="12"
            android:gravity="center_vertical"
            android:layout_gravity="start"
            android:hint="@string/mobile_hint"
            android:inputType="number"
            android:textSize="@dimen/font_size_normal" />
    </android.support.design.widget.TextInputLayout>

</LinearLayout>

这是我的TextInputLayout主题:

<!-- TextInputLayout Style -->
<style name="TextInputLayoutTheme" parent="Widget.Design.TextInputLayout">
    <item name="android:gravity">center</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/colorControlActivated</item>
    <item name="errorTextAppearance">@style/AppTheme.TextErrorAppearance</item>
    <item name="counterTextAppearance">@style/TextAppearance.Design.Counter</item>
    <item name="counterOverflowTextAppearance">@style/TextAppearance.Design.Counter.Overflow</item>
</style>

我尝试搜索许多关键字,术语和方法,但不幸的是没有运气.

I tried searching for many keywords, terms and methods but unfortunately with no luck.

如果有人对此问题有所破解,我将不胜感激.

I would appreciate if anyone has a hack for this issue.

推荐答案

我想出了我的问题,我为AppCompatSpinner设置了边距,并以特定的高度为其设置了最高重力.

I figured out my problem, I set margin to my AppCompatSpinner and set top gravity to it with specific hight.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:orientation="horizontal">

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/spinner_countries"
            style="@style/Widget.AppCompat.Spinner.Underlined"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="26dp"
            android:layout_weight="30"
            android:spinnerMode="dialog" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_mobile"
            style="@style/TextInputLayoutTheme"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            app:errorEnabled="true">

            <com.cashu.app.ui.widgets.CustomEditText
                android:id="@+id/et_mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:ems="12"
                android:gravity="center_vertical"
                android:hint="@string/mobile_hint"
                android:inputType="number"
                android:textSize="@dimen/font_size_normal" />
        </android.support.design.widget.TextInputLayout>

</LinearLayout>

当我的TextInputLayout出现错误时,我通过添加以下行来将其删除:

When my TextInputLayout has an error, I remove it by adding the following lines:

myTextInputLayout.setErrorEnabled(false);
myTextInputLayout.setErrorEnabled(true);
myTextInputLayout.setError(" ");

为什么?

  • 此行:myTextInputLayout.setErrorEnabled(false);将清除TextInputLayout

此行:myTextInputLayout.setErrorEnabled(true);将再次为TextInputLayout启用错误

This line: myTextInputLayout.setErrorEnabled(true); will enable error for the TextInputLayout again

此行:myTextInputLayout.setError(" ");将防止布局对齐中断.

This line: myTextInputLayout.setError(" "); will prevent layout alignment disruption.

问题现在已经解决.

这篇关于AppCompat Spinner的TextInputLayout错误对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 04:30