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