奇怪的TextInputLayout行为

问题描述

我正在尝试在我的textinputlayout中显示可绘制的结尾。但是,可绘制对象以不必要的方式覆盖了textinputlayout的背景(请参见图片Drawable obstructs the background)。

使用轮廓样式时,会发生相同的行为。 后缀的行为方式相同(参见图片Suffix problem)。

此外,错误图标将一直隐藏,直到textinputlayout的edittext中有文本为止(请参见图像No error iconError icon)。 错误图标还显示相同的奇怪视觉行为。

这是我的textinputlayout外观:

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/locationTVLayout"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hapticFeedbackEnabled="true"
                android:hint="Where?"
                app:endIconContentDescription="Find my location."
                app:endIconDrawable="@drawable/ic_baseline_location_searching_24"
                app:endIconMode="custom"
                app:endIconTint="@color/colorAccent"
                app:errorEnabled="true"

                app:startIconDrawable="@drawable/ic_baseline_location_on_24">


                <AutoCompleteTextView
                    android:id="@+id/locationTV"
                    style="@style/Widget.MaterialComponents.AutoCompleteTextView.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:completionThreshold="1"
                    android:layout_below="@id/tempInputTV"
                    android:nextFocusForward="@+id/tempInputTV"
                    android:selectAllOnFocus="true"
                    android:singleLine="true"

                    />
            </com.google.android.material.textfield.TextInputLayout>

我的Material Gradle参考是:

api 'com.google.android.material:material:1.3.0-alpha02'

解决方法

使用app:startIconDrawableapp:endIconDrawable代替android:drawableStartandroid:drawableEnd

这两项都进入您的TextInputLayout,而不是TextInputEditText

有关更多参考,请查看以下内容:https://material.io/develop/android/components/text-fields