我想添加未知号码ImageView
带边距的布局 View 。在 XML 中,我可以使用 layout_margin
像这样:
<ImageView android:layout_margin="5dip" android:src="@drawable/image" />
有ImageView.setPadding()
,但没有ImageView.setMargin()
。我认为这类似于 ImageView.setLayoutParams(LayoutParams)
,但不确定要添加什么内容。
有人知道吗?
请您参考如下方法:
android.view.ViewGroup.MarginLayoutParams
有一个方法 setMargins(left, top, right, Bottom)
。直接子类是:FrameLayout.LayoutParams
、LinearLayout.LayoutParams
和 RelativeLayout.LayoutParams
。
使用例如LinearLayout
:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
这以像素为单位设置边距。要缩放它,请使用
context.getResources().getDisplayMetrics().density