我想创建一个下拉颜色选择器,如下所示(抱歉图像丑陋):
我只需要一些颜色(比方说 6 种),所以我不需要完整的颜色选择器,下拉菜单就可以正常工作。
我知道我必须扩展 Spinner 的阵列适配器并覆盖获取下拉视图 and getView.
我不知道的是如何创建一个带有边框和纯色背景色的方框。
我知道我可以在可绘制对象中定义自己的形状。无论如何,我必须在运行时设置背景颜色,所以我还需要更改视图并设置正确的背景颜色。
哪种方法最好?谢谢。
如果您只想使用背景颜色,可以像这个例子一样使用。
public class CustomSpinnerAdapter<T extends BaseEntity> extends ArrayAdapter implements SpinnerAdapter {
private final List<T> objects; // android.graphics.Color list
public CustomSpinnerAdapter(Context context, List<T> objects) {
super(context, R.layout.yourLayout, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
super.getDropDownView(position, convertView, parent);
View rowView = convertView;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = this.activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.yourLayout, null);
rowView.setBackgroundColor(objects.get(position));
} else {
rowView.setBackgroundColor(objects.get(position));
}
return rowView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = this.activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.yourLayout, null);
rowView.setBackgroundColor(objects.get(position));
} else {
rowView.setBackgroundColor(objects.get(position));
}
return rowView;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)