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

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

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

ImageView.setPadding(),但没有ImageView.setMargin()。我认为它是沿着ImageView.setLayoutParams(LayoutParams)的线,但不确定馈送到它。

有人知道吗?

300287 次浏览

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

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

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

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

你可以使用这个方法,如果你想在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);
}

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

对我来说,这很有效:

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

上面所有的例子实际上都将取代已经为视图呈现的任何参数,这可能不是我们所希望的。下面的代码只会扩展现有的参数,而不会替换它们:

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

我简单地使用这个,工作得很好:

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中设置margin,在你的值/ dimens.xml文件中创建你的维度,如下所示:

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

访问方式如下:

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

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

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

     MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
    
  2. Now just change the margin you want to change but leave the others as they are:

     marginParams.setMargins(marginParams.leftMargin,
    marginParams.topMargin,
    150, //notice only changing right margin
    marginParams.bottomMargin);
    
在某些情况下,使用类似的方法可能会让你省去一些头疼的事情。 如果你有两次对页边距进行编程修补,检查是否已经设置了一些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);
}

如果使用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)

下面是一个在左,上,右,下添加8px Margin的例子。


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


从2020年开始回答:

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

在代码中简单地调用它

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
setMargins(5)
}

在Kotlin中,您可以以更愉快的方式编写它

myView.layoutParams = LinearLayout.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT
).apply {
setMargins(12, 12, 12, 12)
}

我们可以创建线性LayoutParams &dp值使用resources.getDimensionPixelSize

    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)