我正在为自定义NumberPicker尝试复合drawable。我得到的主视图实际上是一个EditText,并且由于EditText有复合drawables(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;
}
}
如果有可用的替代方法,请告诉我。