如何使用代码设置ImageView的边距,而不是xml

我想添加一个未知数量的ImageView视图到我的布局与保证金。 在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

  image = (ImageView) findViewById(R.id.imageID); MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams()); marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams); image.setLayoutParams(layoutParams); 

以上所有的例子都会replace View中已经存在的任何参数,这些参数可能并不需要。 下面的代码将扩展现有的参数,而不会取代它们:

 ImageView myImage = (ImageView) findViewById(R.id.image_view); MarginLayoutParams marginParams = (MarginLayoutParams) image.getLayoutParams(); marginParams.setMargins(left, top, right, bottom); 

凯文的代码创build了冗余的MarginLayoutParams对象。 更简单的版本:

 ImageView image = (ImageView) findViewById(R.id.main_image); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(image.getLayoutParams()); lp.setMargins(50, 100, 0, 0); image.setLayoutParams(lp); 

你可以使用这个方法,如果你想在dp中指定页边距:

 private void addMarginsInDp(View view, int leftInDp, int topInDp, int rightInDp, int bottomInDp) { DisplayMetrics dm = view.getResources().getDisplayMetrics(); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp.setMargins(convertDpToPx(leftInDp, dm), convertDpToPx(topInDp, dm), convertDpToPx(rightInDp, dm), convertDpToPx(bottomInDp, dm)); view.setLayoutParams(lp); } private int convertDpToPx(int dp, DisplayMetrics displayMetrics) { float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics); return Math.round(pixels); } 

如果您想更改imageview边距,但保留所有其他边距不变。

  1. 在这种情况下获取图像视图的myImageViewmyImageView

      MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams(); 
  2. 现在只需更改要更改的保证金,但将其他保留原样:

      marginParams.setMargins(marginParams.leftMargin, marginParams.topMargin, 150, //notice only changing right margin marginParams.bottomMargin); 

我简单地使用它,并且工作得很好:

 ImageView imageView = (ImageView) findViewById(R.id.image_id); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); layoutParams.setMargins(left, top, right, bottom); imageView.setLayoutParams(layoutParams); 

setMargins()的单位是像素不是DP。 如果您想在dp中设置保证金,只需在您的values / dimens.xml文件中创build您的维度,如:

 <resources> <dimen name="right">16dp</dimen> <dimen name="left">16dp</dimen> </resources> 

并访问像:

 getResources().getDimension(R.dimen.right); 

对我来说,这工作:

 int imgCarMarginRightPx = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, definedValueInDp, res.getDisplayMetrics()); MarginLayoutParams lp = (MarginLayoutParams) imgCar.getLayoutParams(); lp.setMargins(0,0,imgCarMarginRightPx,0); imgCar.setLayoutParams(lp); 

dynamic创build布局,并将其参数设置为setmargin()将不会直接在imageView上工作

 ImageView im; im = (ImageView) findViewById(R.id.your_image_in_XML_by_id); RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(im.getLayoutParams()); layout.setMargins(counter*27, 0, 0, 0);//left,right,top,bottom im.setLayoutParams(layout); im.setImageResource(R.drawable.yourimage) 

示例代码在这里,非常简单

 LayoutParams params1 = (LayoutParams)twoLetter.getLayoutParams();//twoletter-imageview params1.height = 70; params1.setMargins(0, 210, 0, 0);//top margin -210 here twoLetter.setLayoutParams(params1);//setting layout params twoLetter.setImageResource(R.drawable.oo); 

如果您使用kotlin,可以通过创build扩展function来简化

 fun View.setMarginExtensionFunction(left: Int, top: Int, right: Int, bottom: Int) { val params = layoutParams as ViewGroup.MarginLayoutParams params.setMargins(left, top, right, bottom) layoutParams = params } 

现在你所需要的只是一个视图,而这个扩展function可以在任何地方使用。

 val imageView = findViewById(R.id.imageView) imageView.setMarginExtensionFunction(0, 0, 0, 0) 

在某些情况下,使用类似于此的方法可能会为您节省一些麻烦。 如果您有两次程序修补边距,检查是否已经设置了一些layoutParams会更安全。 如果已经有一些利润率的话,应该增加它们而不是取代它们:

 public void addMargins(View v, int left, int top, int right, int bottom) { LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams(); if (params == null) params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); int oldLeft = params.leftMargin; int oldTop = params.topMargin; int oldRight = params.rightMargin; int oldBottom = params.bottomMargin; params.setMargins(oldLeft + left, oldTop + top, oldRight + right, oldBottom + bottom); v.setLayoutParams(params); }