3

次のようなドロップダウンカラーピッカーを作成したい(醜い画像で申し訳ありません):

カラー ドロップダウン ピッカー

いくつかの色 (たとえば 6 色) だけが必要なので、完全なカラー ピッカーは必要ありません。ドロップダウンは正常に機能します。

Spinner の配列アダプターを拡張し、getDropDownViewgetViewをオーバーライドする必要があることはわかっています。

私が知らないのは、境界線と単色の背景色を持つ正方形のボックスを作成する方法です。

ドローアブル内で独自の形状を定義できることを知っています。とにかく、実行時に背景色を設定する必要があるため、ビューを変更して正しい背景色を設定する必要もあります。

これを行う最良の方法はどれですか?ありがとう。

4

1 に答える 1

6

背景色だけにしたい場合は、この例のように使用できます。

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;
    }
}
于 2012-10-31T11:55:33.310 に答える