1
我想一个按钮的背景设定为我创造的代码PaintDrawable。这工作得很好,但我的按钮看起来比android.view.Button大。Android:为什么不尊重setPadding?
在下面的图像中,第一按钮是myButton的的一个实例,所述第二按钮是android.widget.Button的一个实例。
我尝试都设置在PaintDrawable和myButton的填充,但它们都没有任何明显的效果。
public class MyButton extends Button
{
PaintDrawable drawable = null;
public ColorButton(Context context)
{
super(context);
drawable = new PaintDrawable();
drawable.getPaint().setColor(Color.WHITE);
drawable.setCornerRadius(1);
//neither of these seem to do anything?
drawable.setPadding(10, 10, 10, 10);
setPadding(10, 10, 10, 10);
setBackgroundDrawable(drawable);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//set gradient in here, because getWidth/getHeight are useless prior to this
drawable.getPaint().setShader(new LinearGradient(getMeasuredWidth()/2, 0, getMeasuredWidth()/2, getMeasuredHeight(), Color.WHITE, Color.GRAY, Shader.TileMode.MIRROR));
}
}
此作品,谢谢。 – ab11 2012-02-16 21:36:16
很高兴帮助! :) – dymmeh 2012-02-16 21:37:02