Android 安卓:为什么不是';难道你不被尊重吗?
我试图将一个按钮的背景设置为我在代码中创建的可绘制的。这很好用,但是我的按钮看起来比android.view.Button大 在下图中,第一个按钮是MyButton的实例,第二个按钮是android.widget.Button的实例 我试着在PaintDrawable和MyButton上设置填充,但都没有明显的效果Android 安卓:为什么不是';难道你不被尊重吗?,android,Android,我试图将一个按钮的背景设置为我在代码中创建的可绘制的。这很好用,但是我的按钮看起来比android.view.Button大 在下图中,第一个按钮是MyButton的实例,第二个按钮是android.widget.Button的实例 我试着在PaintDrawable和MyButton上设置填充,但都没有明显的效果 public class MyButton extends Button { PaintDrawable drawable = null; public Colo
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));
}
}
请注意填充和边距之间的区别 填充确定容器内部的“填充”量。 边距决定容器外部的空间大小 由于您是在空容器上设置填充,因此应该看不到任何可见的结果。 如果你的容器里有东西,当你增加你的值时,你可能会注意到它被压扁了 在你的代码中试试这个。它只在按钮(您的父类)上起作用
//set your fill values to whatever you want
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);
setLayoutParams(lp);