gpt4 book ai didi

Android:为什么不尊重 setPadding?

转载 作者:行者123 更新时间:2023-11-29 18:13:46 25 4
gpt4 key购买 nike

我正在尝试将 Button 的背景设置为我在代码中创建的 PaintDrawable。这很好用,但我的 Button 看起来比 android.view.Button 大。

在下图中,第一个按钮是 MyButton 的实例,第二个按钮是 android.widget.Button 的实例。

我尝试在 PaintDrawable 和 MyButton 上设置填充,但都没有任何明显的效果。

enter image description here

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));
}
}

最佳答案

请注意padding和margin的区别

填充决定了容器内部有多少“填充”。边距决定了在容器外部留出多少空间。

由于您是在一个空容器上设置内边距,因此您应该看不到任何可见的结果。如果您的容器内有东西,您可能会注意到它在您增加值时被压扁

在您的代码中试试这个。它只会作用于按钮(你的父类)

//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);

关于Android:为什么不尊重 setPadding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9318885/

25 4 0
文章推荐: php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP
文章推荐: javascript - 你如何评估 Javascript 中固有的对象方法并用 Big-O 表达它?
文章推荐: java - Spring Criteria 查询数学运算
文章推荐: javascript - 在 之前的多个元素之前搜索