如何在自动完成公开的下拉菜单上设置背景颜色?
How to set background color on autocomplete exposed dropdown menu?
目前正在使用 material 设计组件和菜单指南设置公开的下拉菜单项。但是,当显示我的自动完成文本视图时,它的背景与我的其他文本字段不同。
我已经尝试将 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"/>
我希望背景是透明的或匹配当前设置为白色的背景。目前是灰白色,与字段不匹配。
您在 AutoCompleteTextView 中使用了 style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
作为样式。
将 style.xml 中的浅灰色更改为您的特定颜色。
通过在布局文件中的 TextInputLayout 字段上设置属性 app:boxBackgroundColor="@color/yourcolor"
应该可以正确修改背景颜色。
您可能还需要设置子元素 android:background="@null"
,在我的例子中是 AutoCompleteTextView 以正确遵循 boxBackgroundColor 属性。根据 documentation,您唯一需要设置子元素背景颜色的时间是使用 edittext 字段,但是我发现由于应用程序桥接方式,自动完成视图需要它主题已设置。
感谢 this issue,我了解到这可以通过编程方式完成:
val autoCompleteTv = binding.yourAutoCompleteTextView
autoCompleteTv.setDropDownBackgroundDrawable(
ColorDrawable(ContextCompat.getColor(context, R.color.your_desired_color))
)
目前正在使用 material 设计组件和菜单指南设置公开的下拉菜单项。但是,当显示我的自动完成文本视图时,它的背景与我的其他文本字段不同。
我已经尝试将 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"/>
我希望背景是透明的或匹配当前设置为白色的背景。目前是灰白色,与字段不匹配。
您在 AutoCompleteTextView 中使用了 style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
作为样式。
将 style.xml 中的浅灰色更改为您的特定颜色。
通过在布局文件中的 TextInputLayout 字段上设置属性 app:boxBackgroundColor="@color/yourcolor"
应该可以正确修改背景颜色。
您可能还需要设置子元素 android:background="@null"
,在我的例子中是 AutoCompleteTextView 以正确遵循 boxBackgroundColor 属性。根据 documentation,您唯一需要设置子元素背景颜色的时间是使用 edittext 字段,但是我发现由于应用程序桥接方式,自动完成视图需要它主题已设置。
感谢 this issue,我了解到这可以通过编程方式完成:
val autoCompleteTv = binding.yourAutoCompleteTextView
autoCompleteTv.setDropDownBackgroundDrawable(
ColorDrawable(ContextCompat.getColor(context, R.color.your_desired_color))
)