如何从 android 中的 getDropDownView 方法获取微调器 id 或标签
我有几个微调器,我为它们创建了自定义 ArrayAdapter,这样我就可以更改下拉菜单的外观。我想根据下拉列表所属的微调器来操纵视图。我以为我可以做类似parent.getTag() 的事情,但它返回null。
自定义数组适配器看起来像:
class BackgroundColorAdapter extends ArrayAdapter<String> {
BackgroundColorAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
if(parent.getTag().equals("background"){
//Do custom stuff here
}
return(row);
}
}
我正在设置标签:
settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());
我想我对视图层次结构的工作原理感到困惑,但旋转器下拉列表的父级将是旋转器似乎是合乎逻辑的。有人知道我如何在 getDropDownView 中找到下拉列表属于哪个微调器吗?
编辑:将 settingsSpinner 设为单个微调器而不是一组微调器,以使其不那么混乱
I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.
The custom array adapter looks like:
class BackgroundColorAdapter extends ArrayAdapter<String> {
BackgroundColorAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
if(parent.getTag().equals("background"){
//Do custom stuff here
}
return(row);
}
}
and I'm setting the tag:
settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());
I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?
edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终让它工作起来,这里是示例代码,用于更改下拉列表中每个项目的文本字体。
Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.
我不熟悉 getDropDownView(),也不知道你为什么使用它。 getDropDownView() 的文档说明了有关父级的以下内容:
这听起来不像您正在寻找的“父级”...
由于 getView() 调用中的“父级”确实是一个 Spinner,因此您可以使用它来存储父级的实例变量,如下所示:
我没有尝试过,但也许这是获得您需要的内容的解决方法。如果您找到解决方案,请告诉我。
I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:
This doesn't sound like the 'parent' you are looking for...
Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:
I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.