如何放置在的ImageButton x,y位置? [英] How to place ImageButton at x, y location?
问题描述
我要放置在x的ImageButton,我的观点的y位置。
问题是,Android的添加填充周围的图像。
因为我不知道填充确切大小,我不能确切位置放置图像按钮。
所以,我想删除填充。
如何删除填充图像周围的编程?
button.setPadding(0,0,0,0)时,按钮的宽度更短,高度比位图更长。
button.getLayoutParams()。宽度给出负值。
我试过到目前为止是这样的。
保护类MyLayout扩展RelativeLayout的{
位图IMG;
的ImageButton按钮; 公共MyLayout(上下文的背景下){
按钮=新的ImageButton(背景);
IMG = BitmapFactory.de codeResource(getResources(),R.drawable.img);
button.setImageBitmap(IMG);
PARAMS =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
button.setLayoutParams(PARAMS);
params.setMargins(X,Y,0,0);
button.setBackgroundDrawable(NULL);
addView(按钮,则params);
}
}
修改
使用此...
MarginLayoutParams marginParams =新MarginLayoutParams(image.getLayoutParams());
INT左= someValue中;
INT顶部= someValue中;
marginParams.setMargins(左,顶,0,0);
RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(marginParams);
image.setLayoutParams(的LayoutParams);
I want to place ImageButton at x, y location of my view. The problem is that Android adds padding around image. Because I don't know exact size of padding, I cannot place image button at exact location. So, I want to remove padding. How can I remove padding around image programmatically? button.setPadding(0, 0, 0, 0) makes button width shorter and height longer than bitmap. button.getLayoutParams().width gives minus value. What I tried so far is like this.
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);
}
}
EDIT
Use this...
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);
这篇关于如何放置在的ImageButton x,y位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!