2

我想创建一个下拉颜色选择器,像这样(的丑陋形象不好意思):Android的下拉颜色选择器

color dropdown picker

我只需要一些颜色(假设6),所以我不吨需要一个完整的颜色选择器,下拉将工作正常。

我知道我必须扩展阵列适配器的微调,并覆盖getDropDownViewgetView

我不知道的是如何创建一个带有边框和纯色背景色的正方形框。

我知道我可以在drawable中定义自己的形状。无论如何,我必须在运行时设置背景颜色,所以我还需要更改视图并设置正确的背景颜色。

这是最好的方法吗?谢谢。

+1

您可以动态创建形状绘制...看看[这里](http://www.betaful.com/2012/01/programmatic-shapes-in-android/) –

+0

我用你answed为spinner文本项目的下拉元素和KEYSAN答案。多谢你们。 – Antonio

回答

5

如果你只想使用背景色,你可以像这个例子一样使用。

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; 
    } 
}