【发布时间】:2015-11-20 19:09:25
【问题描述】:
如您在代码中所见,我实现了自己的自定义适配器微调器。 除了图像从左侧附加到每个值而不是仅附加到所选值之外,一切正常。 在下图中,这就是我得到的:
这是我的代码:
public class MyAdapter extends ArrayAdapter<String> {
private Context context;
private String[] values;
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
context = ctx;
values = objects;
}
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
@Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView,ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
false);
TextView subSpinner = (TextView) mySpinner.findViewById(R.id.sub_text_seen);
subSpinner.setText(values[position]);
ImageView right_icon = (ImageView) mySpinner.findViewById(R.id.left_pic);
return mySpinner;
}
}
这是适配器的初始化:
spinnerduration.setAdapter(new MyAdapter(this, R.layout.custom_spinner, weeks_array));
spinnerduration.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
/*TextView selectedText = (TextView) parent.getChildAt(0);
if (selectedText != null) {
selectedText.setTextColor(Color.WHITE);
}*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
/*TextView selectedText = (TextView) parent.getChildAt(0);
if (selectedText != null) {
selectedText.setTextColor(Color.WHITE);
}*/
}
});
【问题讨论】:
-
设置
Spinner背景图片 -
什么意思?而是使用刚刚为微调器设置背景的图像?
-
我应该删除 ImageView 对象吗?并在 clickListener 中设置背景?
-
您必须为
getDropDownView和getView使用两个不同的视图
标签: android