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

Bru*_*Lee 170 android margin imageview

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

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

ImageView.setPadding(),但没有ImageView.setMargin().我认为这是顺利的ImageView.setLayoutParams(LayoutParams),但不确定该提供什么.

有人知道吗?

Key*_*Key 371

android.view.ViewGroup.MarginLayoutParams有一个方法setMargins(left, top, right, bottom).直子类是:FrameLayout.LayoutParams,LinearLayout.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);
Run Code Online (Sandbox Code Playgroud)

MarginLayoutParams

这会设置边距(以像素为单位).要扩展它的使用

context.getResources().getDisplayMetrics().density
Run Code Online (Sandbox Code Playgroud)

DisplayMetrics

  • 请注意,这在PIXELS中设置边距大小,与此问题的xml中的dpi单位不同. (17认同)
  • 您可以使用context.getResources()来缩放px值.getDisplayMetrics().density http://developer.android.com/reference/android/util/DisplayMetrics.html#density (10认同)

小智 48

    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);
Run Code Online (Sandbox Code Playgroud)

  • 与最后两行有什么关系?克隆保证金参数到另一个参数变量?我可以确认它是必需的:) (5认同)

Kel*_*dos 38

以上所有示例实际上都将替换 View中已存在的任何参数,这可能是不可取的.下面的代码将扩展现有的params,而不替换它们:

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

  • 这是此列表中第一个对我有用的解决方案。其他人搞砸了我在 XML 中设置的其他参数,包括用于定位的 RelativeLayout 参数。 (2认同)

Ada*_*zyk 22

凯文的代码创建了冗余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);
Run Code Online (Sandbox Code Playgroud)


Com*_*ode 11

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

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

     MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 现在只需更改您想要更改的边距,但保留原样:

     marginParams.setMargins(marginParams.leftMargin, 
                             marginParams.topMargin, 
                             150, //notice only changing right margin
                             marginParams.bottomMargin); 
    
    Run Code Online (Sandbox Code Playgroud)


div*_*nas 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);
}
Run Code Online (Sandbox Code Playgroud)


eny*_*iaa 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
}
Run Code Online (Sandbox Code Playgroud)

现在你只需要一个视图,这个扩展功能可以在任何地方使用。

val imageView = findViewById(R.id.imageView)
imageView.setMarginExtensionFunction(0, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)


Eln*_*oor 5

我只使用这个并且效果很好:

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

setMargins()的单位是像素而不是dp.如果要在dp中设置margin,只需在values/dimens.xml文件中创建尺寸,如:

<resources>
    <dimen name="right">16dp</dimen>
    <dimen name="left">16dp</dimen>    
</resources>
Run Code Online (Sandbox Code Playgroud)

和访问像:

getResources().getDimension(R.dimen.right);
Run Code Online (Sandbox Code Playgroud)


i30*_*mb1 5

2020年的回答:

dependencies {
    implementation "androidx.core:core-ktx:1.2.0"
}
Run Code Online (Sandbox Code Playgroud)

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

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
   setMargins(5)
}
Run Code Online (Sandbox Code Playgroud)