我目前正在使用 Material Design TextInputLayout OutlinedBox 如下图所示:
<android.support.design.widget.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
我正在尝试添加一个下拉框
Spinner
在我的 TextInputEditText 下,并希望保持相同的样式:OutlinedBox。我看到 Material Design 似乎支持下拉菜单,Material Design Text Fields .如此处所示的区域:
我目前正在使用 Spinner 生成下拉菜单。
<Spinner
style="@style/Widget.AppCompat.Spinner.DropDown"
android:id="@+id/option"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:dropDownWidth="match_parent" />
似乎不可能在 OutlinedBox 设计之后添加下拉菜单。是否有一个库可以让我实现这一点,或者有没有更好的方法在 Material Design 中实现它?
最佳答案
我假设您希望在 TextInputLayout
中拥有一个公开的下拉菜单。我有同样的问题,你可以做的是使用AutoCompleteTextView
在您的TextInputLayout
内如下XML
.这是我如何处理这个问题的一个例子。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingRight="30dp"
android:paddingEnd="30dp"
tools:ignore="RtlSymmetry"
android:layout_margin="5dp">
<ImageView
android:layout_width="30dp"
android:layout_margin="10dp"
android:layout_height="match_parent"
app:srcCompat="@drawable/ic_location_city_black_24dp"
android:layout_gravity="center"
/>
<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="Type"
android:orientation="horizontal"
>
<AutoCompleteTextView
android:id="@+id/filled_exposed_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
您还需要一个项目布局资源来填充下拉弹出窗口。下面的示例提供了一个遵循 Material Design 指南的布局。
res/layout/dropdown_menu_popup_item.xml
<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"/>
根据您的需要,在您的类(class)中添加以下代码。
String[] type = new String[] {"Bed-sitter", "Single", "1- Bedroom", "2- Bedroom","3- Bedroom"};
ArrayAdapter<String> adapter =
new ArrayAdapter<>(
this,
R.layout.dropdown_menu_popup_item,
type);
AutoCompleteTextView editTextFilledExposedDropdown =
findViewById(R.id.filled_exposed_dropdown);
editTextFilledExposedDropdown.setAdapter(adapter);
如果这无济于事,请检查 Material 设计页面中的 Exposed Dropdown Menus。
[ https://material.io/develop/android/components/menu/][1]
这是我关于堆栈溢出的第一个答案,希望对您有所帮助。
关于android - 使用 TextInputLayout.OutlinedBox 样式的 Material 设计 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53198894/