android - 如何在自动完成暴露的下拉菜单上设置背景颜色?

标签 android material-design material-components mdc-components material-components-android

目前正在使用 Material 设计组件和菜单指南设置一个公开的下拉菜单项。但是,当显示我的自动完成 TextView 时,它的背景与我的其他文本字段不同。

我尝试将 TextInputLayout 上的 boxBackgroundColor 属性设置为不同的颜色并修改自动完成 TextView 上的背景。我也厌倦了看看我是否可以修改菜单的弹出主题,但没有运气。

 // In the onCreateView of fragment

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.dropdown_menu_popup_item, getResources().getStringArray(R.array.down_payment_method_selection));
        monthsAtEmploymentSelection.setAdapter(adapter);


<com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:hint="@string/months_at_employment_hint"
                app:boxBackgroundColor="@color/white">

                <AutoCompleteTextView
                    android:id="@+id/months_at_employment_selection"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

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

<!--dropdown_menu_popup_item.xml-->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:textAppearance="?attr/textAppearanceSubtitle1"
    android:background="@color/white"/>

我希望背景是透明的或与当前设置为白色的背景相匹配。目前它是与字段不匹配的灰白色。

Example of color problems when using extended dropdown menu style

最佳答案

通过设置属性 app:boxBackgroundColor="@color/yourcolor"在布局文件中的 TextInputLayout 字段上应正确修改背景颜色。

您可能还需要设置子元素 android:background="@null" ,在我的情况下 AutoCompleteTextView 正确地尊重 boxBackgroundColor 属性。根据 documentation您需要设置子元素背景颜色的唯一时间是使用edittext字段,但是由于应用程序桥主题的设置方式,我发现我需要使用自动完成 View 来设置它。

关于android - 如何在自动完成暴露的下拉菜单上设置背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56638427/

相关文章:

android - 如何更改android中工具栏上关闭搜索按钮的位置并在展开时设置动画?

android - 较小的自定义 RatingBar : Material Design

android - 当蓝牙连接到我的设备时,是否可以在应用程序中收到通知?

android - kotlin.TypeCastException : null cannot be cast to non-null type com. midsizemango.databasekotlin.Note

Android - 服务 - 启动命令仅调用一次

java - Android使用指纹解密文件

android - 在 Android 中 fragment 发生变化时如何使用 Material Design Transition?

Android 芯片 View 不使用 android :textAllCaps ="true" 大写字母

android - 无法使用 Material 组件库设置按钮的背景颜色

android - 在滚动时显示和隐藏 BottomAppBar