我想添加未知号码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.LayoutParamsLinearLayout.LayoutParamsRelativeLayout.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); 

MarginLayoutParams

这以像素为单位设置边距。要缩放它,请使用

context.getResources().getDisplayMetrics().density 

DisplayMetrics


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!