Ant*_*nio 2 android color-picker android-arrayadapter android-spinner
我想创建一个下拉颜色选择器,像这样(抱歉丑陋的图像):

我只需要一些颜色(让我们说6)所以我不需要一个完整的颜色选择器,下拉列表将正常工作.
我知道我必须为Spinner扩展数组适配器并覆盖getDropDownView和getView.
我不知道的是如何创建一个带边框和纯色背景的方框.
我知道我可以在drawable中定义自己的形状.无论如何,我必须在运行时设置背景颜色,所以我还需要更改视图并设置正确的背景颜色.
这是最好的方法吗?谢谢.
如果您只想要背景颜色,可以像这个例子一样使用.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3559 次 |
| 最近记录: |