2012-02-16 58 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

相關問題