ボタンの背景をコードで作成しているPaintDrawableに設定しようとしています。これはかなりうまくいくが、私のボタンはandroid.view.Buttonよりも大きく見える。下の画像でAndroid:なぜsetPaddingは尊重されていないのですか?
、最初のボタンは、第二のボタンはandroid.widget.Buttonのインスタンスである、MyButtonというのインスタンスです。
私は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
お手伝いしてうれしい! :) – dymmeh