Android自定义微调器每种颜色

Android自定义微调器每种颜色,android,row,spinner,Android,Row,Spinner,我想在微调器中有一个颜色列表,我该怎么办?我不想将RGB代码存储在数据库中,您可以直接从XML进行存储吗?您推荐哪种方式?您可以使用如下自定义的适配器: private class ExampleAdapter extends ArrayAdapter<String> { private final int[] colors = new int [] { Color.WHITE, Color.RED,

我想在微调器中有一个颜色列表,我该怎么办?我不想将RGB代码存储在数据库中,您可以直接从XML进行存储吗?您推荐哪种方式?

您可以使用如下自定义的
适配器:

private class ExampleAdapter extends ArrayAdapter<String> {

    private final int[] colors = new int [] {
            Color.WHITE,
            Color.RED,
            Color.BLUE
    };

    private ExampleAdapter(Context context, int resource) {
        super(context, resource);
    }

    private ExampleAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public ExampleAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
    }

    private ExampleAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    private ExampleAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    private ExampleAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        int color = getColorForPosition(position);
        view.setBackgroundColor(color);

        return view;
    }

    private int getColorForPosition(int position) {
        return colors[position % colors.length];
    }
}
私有类ExampleAdapter扩展了ArrayAdapter{
私有最终整数[]颜色=新整数[]{
颜色,白色,
颜色,红色,
蓝色
};
私有ExampleAdapter(上下文,int资源){
超级(上下文、资源);
}
私有ExampleAdapter(上下文上下文、int资源、int textViewResourceId){
超级(上下文、资源、textViewResourceId);
}
公共示例适配器(上下文、int资源、字符串[]对象){
超级(上下文、资源、对象);
}
私有ExampleAdapter(上下文上下文、int资源、int textViewResourceId、字符串[]对象){
超级(上下文、资源、textViewResourceId、对象);
}
私有ExampleAdapter(上下文、int资源、列表对象){
超级(上下文、资源、对象);
}
私有ExampleAdapter(上下文上下文、int资源、int textViewResourceId、列表对象){
超级(上下文、资源、textViewResourceId、对象);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
int color=getColorForPosition(位置);
视图。setBackgroundColor(颜色);
返回视图;
}
私有整数getColorForPosition(整数位置){
返回颜色[位置%colors.length];
}
}

您可以像使用任何
ArrayAdapter
一样使用此
ExampleAdapter
,但项目将在顶部定义的背景色中循环。

您必须创建一个自定义
适配器才能执行此操作。