我想在我的布局中添加未知数量的ImageView
次视图,并带有边距。在XML中,我可以像这样使用layout_margin
:
<ImageView android:layout_margin="5dip" android:src="@drawable/image" />
有ImageView.setPadding()
,但没有ImageView.setMargin()
。我认为它符合ImageView.setLayoutParams(LayoutParams)
的范围,但不确定该提供什么。
有人知道吗?
答案 0 :(得分:358)
android.view.ViewGroup.MarginLayoutParams
有一个方法setMargins(left, top, right, bottom)
。直接子类包括:FrameLayout.LayoutParams
,LinearLayout.LayoutParams
和RelativeLayout.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
答案 1 :(得分:46)
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);
答案 2 :(得分:35)
以上所有示例实际上都会 REPLACE 为视图提供的任何参数,这可能是不合需要的。以下代码将扩展现有的参数,而不替换它们:
Make Model Year Sales
Toyota Corolla 2014 15
Toyota Camry 2014 12
Toyota RAV4 2014 4
答案 3 :(得分:21)
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);
答案 4 :(得分:10)
如果您想更改图像视图边距,但保留所有其他边距。
在这种情况下获取图片视图的MarginLayoutParameters:myImageView
MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
现在只需更改您想要更改的保证金,但保留原来的保证金:
marginParams.setMargins(marginParams.leftMargin,
marginParams.topMargin,
150, //notice only changing right margin
marginParams.bottomMargin);
答案 5 :(得分: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);
}
答案 6 :(得分: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);
setMargins()的单位是像素而不是dp。如果要在dp中设置边距,只需在 values / dimens.xml 文件中创建尺寸,如:
<resources>
<dimen name="right">16dp</dimen>
<dimen name="left">16dp</dimen>
</resources>
并访问如下:
getResources().getDimension(R.dimen.right);
答案 7 :(得分: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)
答案 8 :(得分: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);
答案 9 :(得分:4)
如果您使用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)
答案 10 :(得分: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);
答案 11 :(得分:2)
从2020年开始的答案:
dependencies {
implementation "androidx.core:core-ktx:1.2.0"
}
并在您的代码中对其进行校准
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
setMargins(5)
}
答案 12 :(得分: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);
}
答案 13 :(得分: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);
答案 14 :(得分:0)
在Kotlin中,您可以用更愉快的方式编写
myView.layoutParams = LinearLayout.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT
).apply {
setMargins(12, 12, 12, 12)
}
答案 15 :(得分:0)
我们可以创建线性LayoutParams ,并使用 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)