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

问题描述 投票:176回答:14

我想给我的布局添加未知数量的ImageView视图,并留有边距。在XML中,可以这样使用layout_margin

<ImageView android:layout_margin="5dip" android:src="@drawable/image" />

ImageView.setPadding(),但没有ImageView.setMargin()。我认为这是与ImageView.setLayoutParams(LayoutParams)相似的,但不确定该输入什么。

有人知道吗?

android margin imageview
14个回答
380
投票

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


50
投票
    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);

42
投票

以上所有示例实际上将REPLACE对于View已经存在的任何参数,这可能是不希望的。下面的代码将扩展现有的参数,而不替换它们:

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

22
投票

Kevin的代码创建了冗余的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);

11
投票

如果要更改图像视图边距,但保留所有其他边距不变。

  1. 在这种情况下,获取图像视图的MarginLayoutParameters:myImageView

     MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
    
  2. 现在只需更改要更改的边距,而其他保留不变:

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

8
投票

如果要在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);
}

8
投票

我只是简单地使用它,效果很好:

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文件内部创建尺寸,例如:

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

和访问方式:

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

5
投票

如果使用kotlin,则可以通过创建扩展功能来简化

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
}

现在您只需要一个视图,并且此扩展功能可以在任何地方使用。

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

4
投票

动态创建布局并将其参数设置为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)

4
投票

对我来说这很有效:

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);

2
投票

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

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);

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);
}

0
投票

这里是在左侧,顶部,右侧,底部添加8px页边距的示例。


ImageView imageView = new ImageView(getApplicationContext());

ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(
    ViewGroup.MarginLayoutParams.MATCH_PARENT,
    ViewGroup.MarginLayoutParams.WRAP_CONTENT
);

marginLayoutParams.setMargins(8, 8, 8, 8);

imageView.setLayoutParams(marginLayoutParams);


0
投票

2020年起的答案:

dependencies {
    implementation "androidx.core:core-ktx:1.2.0"
}

只需在您的代码中对其进行校准

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
   setMargins(5)
}
© www.soinside.com 2019 - 2024. All rights reserved.