将任何android视图添加到自定义视图

时间:2012-11-22 09:37:03

标签: android custom-component seekbar

我在CustomDialog中有一个简单的视图:

public class ColorPickerDialog extends Dialog 
{
    private static class ColorPickerView extends View
    {
        ColorPickerView(Context c, int color) 
        {
             super(c);
             //...
        }
        @Override 
        protected void onDraw(Canvas canvas) {
            //...
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
        }
    }

    public ColorPickerDialog(Context context,
                             int initialColor) {
        super(context);
        mInitialColor = initialColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new ColorPickerView(getContext(), mInitialColor));
    }
}

如何添加到此视图SeekBar?

1 个答案:

答案 0 :(得分:1)

您可以为颜色选择器和搜索条创建容器类。

例如,如果要垂直布局它们:

private static class ColorPickerContainer extends LinearLayout {

    private ColorPickerView colorPicker;
    private SeekBar seekBar;

    public ColorPickerContainer(Context context, int initialColor) {
        super(context);
        setOrientation(LinearLayout.VERTICAL);

        colorPicker = new ColorPickerView(context, initialColor);
        addView(colorPicker);

        seekBar = new SeekBar(context);
        addView(seekBar);
    }

    public ColorPickerView getColorPicker() {
        return colorPicker;
    }

    public SeekBar getSeekBar() {
        return seekBar;
    }
}