如何使用代码而不是xml设置ImageView的边距
我想将未知数量的 ImageView
视图添加到带有边距的布局中。在 XML 中,我可以像这样使用 layout_margin
:
有 <代码>ImageView.setPadding(),但没有ImageView.setMargin()
。我认为这与 ImageView.setLayoutParams(LayoutParams) 类似,但不确定要输入什么内容。
有谁知道吗?
I want to add an unknown number of ImageView
views to my layout with margin. In XML, I can use layout_margin
like this:
<ImageView android:layout_margin="5dip" android:src="@drawable/image" />
There is ImageView.setPadding()
, but no ImageView.setMargin()
. I think it's along the lines of ImageView.setLayoutParams(LayoutParams)
, but not sure what to feed into that.
Does anyone know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您想更改图像视图边距但保持所有其他边距不变。
在本例中获取图像视图的 MarginLayoutParameters:
myImageView
现在只需更改您想要更改的边距,但保持其他不变:
If you want to change imageview margin but leave all other margins intact.
Get MarginLayoutParameters of your image view in this case:
myImageView
Now just change the margin you want to change but leave the others as they are:
2020 年的答案:
并在代码中简单地进行计算
Answer from 2020 year :
and cal it simply in your code
我简单地使用这个并且效果很好:
setMargins() 的单位是像素而不是 dp。如果您想以 dp 为单位设置边距,请在您的 values/dimens.xml 文件中创建您的尺寸,如:
并访问如下:
I use simply this and works great:
setMargins()'s unit is pixel not dp. If you want to set margin in dp, just inside your values/dimens.xml file create your dimensions like:
and access like:
如果您使用 kotlin,则可以通过创建扩展函数来简化
现在您所需要的只是一个视图,并且该扩展函数可以在任何地方使用。
If you use kotlin, this can be simplified by creating an extension function
Now all you need is a view, and this extension function can be used anywhere.
如果您想以 dp 指定边距,可以使用此方法:
You can use this method, in case you want to specify margins in dp:
对我来说这有效:
For me this worked:
在 Kotlin 中你可以用更愉快的方式编写它
In Kotlin you can write it in more pleasant way
下面是在左、上、右、下添加 8px 边距的示例。
Here is an example to add 8px Margin on left, top, right, bottom.
在某些情况下,使用与此类似的方法可能会为您省去一些麻烦。
如果您对边距进行了两次编程修改,那么检查是否已经设置了一些布局参数会更安全。如果已经有一些边距,则应该增加它们而不是替换它们:
Using a method similar to this might save you some headaches in some situations.
If you have two passes of programmatical tinkering with margins it is safer to check if there are already some layoutParams set. If there are some margins already one should increase them and not replace them:
我们可以创建Linear LayoutParams &使用 resources.getDimensionPixelSize 作为 dp 值。
We can create Linear LayoutParams & use resources.getDimensionPixelSize for dp value.
android.view.ViewGroup.MarginLayoutParams
有一个方法setMargins(left, top, right, Bottom)
。直接子类是:FrameLayout.LayoutParams、LinearLayout.LayoutParams 和RelativeLayout.LayoutParams。使用例如
LinearLayout
:MarginLayoutParams
这设置边距(以像素为单位)。要缩放它,请使用
DisplayMetrics
android.view.ViewGroup.MarginLayoutParams
has a methodsetMargins(left, top, right, bottom)
. Direct subclasses are:FrameLayout.LayoutParams
,LinearLayout.LayoutParams
andRelativeLayout.LayoutParams
.Using e.g.
LinearLayout
:MarginLayoutParams
This sets the margins in pixels. To scale it use
DisplayMetrics
上面的所有示例实际上都会替换视图中已经存在的任何参数,这可能是不需要的。以下代码将仅扩展现有参数,而不替换它们:
All the above examples will actually REPLACE any params already present for the View, which may not be desired. The below code will just extend the existing params, without replacing them:
Kevin 的代码创建了冗余的
MarginLayoutParams
对象。更简单的版本:Kevin's code creates redundant
MarginLayoutParams
object. Simpler version: