195

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

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

ImageView.setPadding(),但没有ImageView.setMargin()。我认为它是这样的ImageView.setLayoutParams(LayoutParams),但不知道该输入什么。

有人知道吗?

4

16 回答 16

400

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

边距布局参数

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

context.getResources().getDisplayMetrics().density

显示指标

于 2010-08-05T15:19:26.287 回答
53
    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);
于 2011-03-08T07:18:20.850 回答
45

上述所有示例实际上将替换视图中已经存在的任何参数,这可能是不需要的。以下代码将仅扩展现有参数,而不替换它们:

ImageView myImage = (ImageView) findViewById(R.id.image_view);
MarginLayoutParams marginParams = (MarginLayoutParams) image.getLayoutParams();
marginParams.setMargins(left, top, right, bottom);
于 2015-09-04T09:26:16.983 回答
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);
于 2013-06-15T15:02:48.647 回答
12

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

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

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

     marginParams.setMargins(marginParams.leftMargin, 
                             marginParams.topMargin, 
                             150, //notice only changing right margin
                             marginParams.bottomMargin); 
    
于 2016-02-18T19:27:24.503 回答
9

我简单地使用它并且效果很好:

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);
于 2015-11-17T04:27:37.170 回答
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);
}
于 2015-02-05T09:26:09.613 回答
8

如果你使用 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)
于 2017-09-02T17:51:49.783 回答
5

2020年的答案:

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

并在您的代码中简单地调用它

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
   setMargins(5)
}
于 2020-05-08T13:11:26.153 回答
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)
于 2015-04-27T16:56:18.910 回答
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);
于 2015-06-02T16:19:05.270 回答
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);
于 2013-10-19T12:19:24.993 回答
0

在 Kotlin 中你可以用更愉快的方式编写它

myView.layoutParams = LinearLayout.LayoutParams(
                RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT
            ).apply {
                setMargins(12, 12, 12, 12)
            }
于 2020-07-30T11:43:06.393 回答
0

我们可以创建线性布局参数并使用resources.getDimensionPixelSize作为 dp 值。

    val mContext = parent.context
    val mImageView = AppCompatImageView(mContext)
    mImageView.setBackgroundResource(R.drawable.payment_method_selector)

    val height = mContext.resources.getDimensionPixelSize(R.dimen.payment_logo_height)
    val width = mContext.resources.getDimensionPixelSize(R.dimen.payment_logo_width)
    val padding = mContext.resources.getDimensionPixelSize(R.dimen.spacing_small_tiny)
    val margin = mContext.resources.getDimensionPixelSize(R.dimen.spacing_small)

    mImageView.layoutParams = LinearLayout.LayoutParams(width, height).apply {
        setMargins(margin, margin, 0, 0)
    }
    mImageView.setPadding(padding, padding, padding, padding)
于 2020-09-17T11:50:32.177 回答
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);

于 2019-02-18T10:53:29.350 回答
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);
}
于 2017-07-03T01:14:00.720 回答