Android设置页边距无法以编程方式工作
ImageView仍然有marginLeft 10和marginTop 11 怎么了?尝试使用设置Android设置页边距无法以编程方式工作,android,margin,Android,Margin,ImageView仍然有marginLeft 10和marginTop 11 怎么了?尝试使用设置图像视图的边距。这里有一个非常适合我的方法 ImageView imageView = (ImageView) findViewRoot(R.id.imageView1); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); params.width = 100
图像视图的边距。这里有一个非常适合我的方法
ImageView imageView = (ImageView) findViewRoot(R.id.imageView1);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width = 100;
params.height = 100;
params.setMargins(0,0,0,0); //NOT WORK
imageView.setLayoutParams(params);
如何在代码中使用的示例:
private void setMargins (View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(left, top, right, bottom);
view.requestLayout();
}
}
我的测试布局:
public void testMargin(View view){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width = 100;
params.height = 100;
setMargins(imageView , 0, 0, 0, 0);
imageView.setLayoutParams(params);
}
" />
希望这有帮助!!你有没有试着做一个新的布局参数
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
tools:context="com.g2o.test.TestActivity">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:background="@android:color/holo_red_dark" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:adjustViewBounds="true"
android:background="@drawable/cast_ic_notification_0" />
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="testMargin"
android:text="Test" />" />
</RelativeLayout>
希望这有助于将您的ImageView置于相对位置,并将其宽度和高度设置为与父视图匹配
ImageView imageView = (ImageView) findViewRoot(R.id.imageView1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0,0,0,0);
imageView.setLayoutParams(params);
//注意:展开相对布局,而不是图像本身这可能对某些人有用:
在我的例子中,我处理的是同一个问题,android api 19无法识别边距,因此我所做的是更改图像的大小(增加一点),然后调整填充,如下所示:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, 400);
lay_layout.setLayoutParams(params);
opcionCroptoppadding允许您在边距不起作用的情况下进行变通
我希望这会有帮助。你的问题解决了吗?没有。两个答案都没有解决我的问题。试着在没有得到旧参数的情况下设置参数。请多解释一点。这不起作用。什么都不要做。仍然保留10和11的边缘可能是因为我也更改了尺寸?我的父布局是相对布局。没有这样的内容我测试了你的样本,效果很好。我用我的测试布局和活动编辑了我的答案,我还添加了android:adjustViewBounds=“true”“
使ImageView适合实际图像大小,以避免任何额外的边距。如果这对你不起作用,请告诉我。好的,如果你还有问题,请告诉我。这不起作用。什么都没有发生,仍然有相同的利润
<RelativeLayout
android:id="@+id/lay_expandable"
android:layout_width="match_parent"
android:layout_height="200dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_expansion" />
</RelativeLayout>
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, 400);
lay_layout.setLayoutParams(params);
file: dimens.xml
<dimen name="image_padding_right">5dp</dimen>
val imageView1 = ImageView(this.activity)
imageView1.setPadding(0,0,
resources.getDimension(R.dimen.image_padding_right).roundToInt(), 0)
imageView1.cropToPadding = true