0

ビューのx、yの位置にImageButtonを配置したいと思います。問題は、Androidが画像の周りにパディングを追加することです。パディングの正確なサイズがわからないため、画像ボタンを正確な位置に配置できません。だから、私はパディングを削除したいと思います。プログラムで画像の周りのパディングを削除するにはどうすればよいですか?button.setPadding(0、0、0、0)は、ボタンの幅をビットマップより短くし、高さを長くします。button.getLayoutParams()。widthはマイナスの値を示します。今までやってみたのはこんな感じ。

protected class MyLayout extends RelativeLayout {
    Bitmap img;
    ImageButton button;

    public MyLayout(Context context) {
        button = new ImageButton(context);
        img = BitmapFactory.decodeResource(getResources(), R.drawable.img); 
        button.setImageBitmap(img);
        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        params.setMargins(x, y, 0, 0);
        button.setBackgroundDrawable(null);
        addView(button, params);
    }
}       
4

1 に答える 1

0

編集

これを使って...

     MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());  
     int left = someValue;
     int top = someValue;
     marginParams.setMargins(left, top, 0, 0);
     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
     image.setLayoutParams(layoutParams);   
于 2012-04-23T16:15:24.327 に答える