2012-02-16 13 views
1

ボタンの背景をコードで作成しているPaintDrawableに設定しようとしています。これはかなりうまくいくが、私のボタンはandroid.view.Buttonよりも大きく見える。下の画像でAndroid:なぜsetPaddingは尊重されていないのですか?

、最初のボタンは、第二のボタンはandroid.widget.Buttonのインスタンスである、MyButtonというのインスタンスです。

私は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

+0

お手伝いしてうれしい! :) – dymmeh

関連する問題