开发者

Setting margins for an imageview dynamically

开发者 https://www.devze.com 2023-03-28 18:47 出处:网络
may I know how to set margi开发者_开发问答n in imageview dynamically?You\'re probably looking for something like this: http://developer.android.com/reference/android/view/View.html#setLayoutParams(and

may I know how to set margi开发者_开发问答n in imageview dynamically?


You're probably looking for something like this: http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

Note this part of the method description though:

These supply parameters to the parent of this view specifying how it should be arranged

Which means that if you have an ImageView inside of a LinearLayout, you need to supply the method with LinearLayout.LayoutParams, like this:

ImageView image = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
params.setMargins(1, 1, 1, 1);
image.setLayoutParams(params);

And then you just call setMargins or set the specific leftMargin, bottomMargin etc. properties of the LayoutParams.

0

精彩评论

暂无评论...
验证码 换一张
取 消