这正是你所需要的
https://material.io/develop/android/components/menu/#exposed-dropdown-menus
首先在 Textinputlayout 中添加 AutocompleteTextView
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_text">
<AutoCompleteTextView
android:id="@+id/filled_exposed_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
然后你可以像这样设计菜单项:
<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"/>
在java中初始化适配器,如:
String[] COUNTRIES = new String[] {"Item 1", "Item 2", "Item 3", "Item 4"};
ArrayAdapter<String> adapter =
new ArrayAdapter<>(
getContext(),
R.layout.dropdown_menu_popup_item,
COUNTRIES);
AutoCompleteTextView editTextFilledExposedDropdown =
view.findViewById(R.id.filled_exposed_dropdown);
editTextFilledExposedDropdown.setAdapter(adapter);
您可以更改样式以满足各种变化,例如:
填充
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
概述
将此样式应用于您的 TextInputLayout:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
密集填充
将此样式应用于您的 TextInputLayout:
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense.ExposedDropdownMenu"
密集轮廓
将此样式应用于您的 TextInputLayout:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"