2012-02-16 77 views
1

我想一个按钮的背景设定为我创造的代码PaintDrawable。这工作得很好,但我的按钮看起来比android.view.Button大。Android:为什么不尊重setPadding?

在下面的图像中,第一按钮是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)); 
    } 
} 

回答

3

请注意

填充的多少决定了“填充”是在你的容器内填充和利润之间的差额。 边距决定了在容器外部留出多少空间。

因为你是一个空的容器上设置填充你应该看不到明显的效果。 如果你有你的容器里面的东西,你可能会注意到它,你提高你的价值观

在你的代码试试这个越来越压扁。它只会在按钮上工作(您的父级)

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

此作品,谢谢。 – ab11 2012-02-16 21:36:16

+0

很高兴帮助! :) – dymmeh 2012-02-16 21:37:02

相关问题