问题描述
我可以知道如何在imageview中动态设置边距吗?
may I know how to set margin in imageview dynamically?
推荐答案
您可能正在寻找类似以下内容: http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)
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:
这意味着,如果您在LinearLayout中具有ImageView,则需要为该方法提供LinearLayout.LayoutParams,如下所示:
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);
然后您只需调用setMargins或设置LayoutParams的特定leftMargin,bottomMargin等属性即可.
And then you just call setMargins or set the specific leftMargin, bottomMargin etc. properties of the LayoutParams.
这篇关于动态设置imageview的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!