3

我正在尝试为我的自定义 NumberPicker 提供复合可绘制对象。我知道主视图实际上是一个 EditText,并且由于 EditText 具有复合可绘制对象(DrawableLeft、DrawableRight 等),我尝试设置它,但是当 NumberPicker 实际出现时,它从那里不存在。还有什么我可以尝试使它工作的吗?

我当前的代码 -

public class ViewPicker extends NumberPicker {
    private int textSize = -1;

    public ViewPicker(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public ViewPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public ViewPicker(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        updateView(child);
    }

    @Override
    public void addView(View child, int index,
            android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        updateView(child);
    }

    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        updateView(child);
    }

    private void updateView(View view) {
        if (view instanceof EditText) {
            EditText editText = (EditText) view;
            if (textSize > 0)
                editText.setTextSize(textSize);
            editText.setTextColor(Color.parseColor("#333333"));

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

            BitmapDrawable draw = new BitmapDrawable(getResources(),bitmap);

            editText.setCompoundDrawables(draw, null, null, null);
        }
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }
}

让我知道是否有任何替代方法可用。

4

0 回答 0