如何确认截图对应的Android控件在Android Studio中的名称
问题
我是Android开发新手,若有表述不周之处还请见谅...
请问图中所示控件在Android Studio中的对应名称是什么?我此前尝试通过谷歌检索相关内容,但始终没能找到准确结果。

回答
这个是Material Design组件库提供的暴露式下拉菜单(Exposed Dropdown Menu),不是Android SDK原生的独立基础控件,很多人搜不到是因为它是组合实现的控件,不存在单一的对应控件类名:
- 外层用
TextInputLayout作为容器,必须指定ExposedDropdownMenu对应的样式 - 内部嵌套
AutoCompleteTextView(推荐用Material库封装的MaterialAutoCompleteTextView)承载下拉选择逻辑 - 控件默认自带下拉箭头,点击输入区域弹出选项列表,样式比原生老旧的
Spinner更符合现代UI规范
使用前提:
- 在模块的
build.gradle中引入Material Components依赖 - 项目App主题继承自MaterialComponents系列主题,否则控件会出现样式错乱
基础布局写法参考:
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请选择"> <com.google.android.material.textfield.MaterialAutoCompleteTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="none" android:focusable="false"/> </com.google.android.material.textfield.TextInputLayout>
给控件绑定下拉选项的逻辑和普通AutoCompleteTextView完全一致,设置对应ArrayAdapter即可。
内容的提问来源于stack exchange,提问作者Learn2Code
相关产品推荐
相关产品推荐

