Android 如何使用代码而不是 xml 设置 ImageView 的边距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3416087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set margin of ImageView using code, not xml
提问by Bruce Lee
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
用边距向我的布局添加未知数量的视图。在 XML 中,我可以这样使用layout_margin
:
<ImageView android:layout_margin="5dip" android:src="@drawable/image" />
<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.
有ImageView.setPadding()
,但没有ImageView.setMargin()
。我认为它与ImageView.setLayoutParams(LayoutParams)
,但不知道该输入什么。
Does anyone know?
有人知道吗?
回答by Key
android.view.ViewGroup.MarginLayoutParams
has a method setMargins(left, top, right, bottom)
. Direct subclasses are: FrameLayout.LayoutParams
, LinearLayout.LayoutParams
and RelativeLayout.LayoutParams
.
android.view.ViewGroup.MarginLayoutParams
有方法setMargins(left, top, right, bottom)
。直接子类是:FrameLayout.LayoutParams
,LinearLayout.LayoutParams
和RelativeLayout.LayoutParams
。
Using e.g. LinearLayout
:
使用例如LinearLayout
:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
This sets the margins in pixels. To scale it use
这以像素为单位设置边距。要扩展它使用
context.getResources().getDisplayMetrics().density
回答by Kevin Wadera
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);
回答by Kelevandos
All the above examples will actually REPLACEany params already present for the View, which may not be desired. The below code will just extend the existing params, without replacing them:
上面的所有示例实际上都会替换View 已经存在的任何参数,这可能不是我们想要的。下面的代码只会扩展现有的参数,而不替换它们:
ImageView myImage = (ImageView) findViewById(R.id.image_view);
MarginLayoutParams marginParams = (MarginLayoutParams) image.getLayoutParams();
marginParams.setMargins(left, top, right, bottom);
回答by Adam Stelmaszczyk
Kevin's code creates redundant MarginLayoutParams
object. Simpler version:
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);
回答by CommonSenseCode
If you want to change imageview margin but leave all other margins intact.
如果您想更改图像视图边距但保留所有其他边距不变。
Get MarginLayoutParameters of your image view in this case:
myImageView
MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
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);
在这种情况下获取图像视图的 MarginLayoutParameters:
myImageView
MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
现在只需更改您想要更改的边距,但保持其他不变:
marginParams.setMargins(marginParams.leftMargin, marginParams.topMargin, 150, //notice only changing right margin marginParams.bottomMargin);
回答by Elnoor
I use simply this and works great:
我简单地使用这个并且效果很好:
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()'s unit is pixel not dp. If you want to set margin in dp, just inside your values/dimens.xmlfile create your dimensions like:
setMargins()的单位是像素而不是 dp。如果你想在 dp 中设置边距,就在你的values/dimens.xml文件中创建你的尺寸,如:
<resources>
<dimen name="right">16dp</dimen>
<dimen name="left">16dp</dimen>
</resources>
and access like:
并访问如:
getResources().getDimension(R.dimen.right);
回答by divonas
You can use this method, in case you want to specify margins in dp:
如果要在 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);
}
回答by enyciaa
If you use kotlin, this can be simplified by creating an extension function
如果你使用 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
}
Now all you need is a view, and this extension function can be used anywhere.
现在你只需要一个视图,这个扩展功能可以在任何地方使用。
val imageView = findViewById(R.id.imageView)
imageView.setMarginExtensionFunction(0, 0, 0, 0)
回答by Muhammad Adil
create layout dynamically and set its parameter as setmargin() will not work directly on an imageView
动态创建布局并将其参数设置为 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)
回答by Wahib Ul Haq
For me this worked:
对我来说这有效:
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);