2012-04-23 82 views
0

我想将ImageButton放置在我的视图的x,y位置。 问题是Android在图像周围添加了填充。 因为我不知道填充的确切大小,所以我无法将图像按钮放置在精确的位置。 所以,我想删除填充。 如何以编程方式去除图像周围的填充? button.setPadding(0,0,0,0)使按钮宽度比位图更短且高度更长。 button.getLayoutParams()。width给出负值。 我到目前为止所尝试的是这样的。如何将ImageButton放置在x,y位置?

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); 
    } 
}  

回答

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

但他怎么能使用相同的params.setMargins(X,Y,0将其设置在特定的位置 – 2012-04-23 16:24:39

+0

,0 ); – 2012-04-23 16:25:36

+0

然后为什么要删除它 – 2012-04-23 16:27:23

相关问题