Android setMargins程序上不起作用

5
<RelativeLayout>

    ...

    <ImageView
      android:id="@+id/imageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginLeft="10dp"
      android:layout_marginTop=”11dp”
      android:background=”@drawable/image”  />

</RelativeLayout>

接下来我需要通过编程方式设置边距:

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);

ImageView仍然具有marginLeft 10和marginTop 11;
有什么问题吗?

你的问题解决了吗? - Gueorgui Obregon
不是。两个答案都没有解决我的问题。 - user3240604
尝试在不获取旧参数的情况下设置参数。 - Keshav
请再解释一下。 - user3240604
在对话框显示后设置边距。 - undefined
5个回答

0

尝试使用ViewGroup.MarginLayoutParams为您的ImageView设置边距。这里是@Hiren Patel答案中的一个方法,对我来说非常完美。

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>

希望这能有所帮助!

这个不起作用。什么也没做。仍然有10和11的边距。 - user3240604
可能是因为我改变了大小吗? - user3240604
我的父布局是相对布局。没有任何东西。 - user3240604
我测试了你的示例,对我来说运行良好。我 编辑了我的答案,加入了我的测试布局和活动,我还添加了 android:adjustViewBounds="true" 以适应 ImageView 的实际图像大小,避免任何额外的边距。如果这对你不起作用,请告诉我。 - Gueorgui Obregon
好的,请告诉我您是否仍然遇到问题。 - Gueorgui Obregon

0

你尝试过创建新的LayoutParams吗?

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);

这不起作用。什么也没有发生,边距仍然相同。 - user3240604

0
希望这可以帮助您将ImageView放入RelativeLayout中,并将其宽度和高度设置为Match-Parent。
 <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 例如

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, 400);
            lay_layout.setLayoutParams(params);

// 请注意扩展相对布局,而不是图像本身


0
这可能对某些人有用: 在我的情况下,我遇到了同样的问题,即在Android API 19上无法识别边距,所以我所做的是改变图像的大小(稍微增加),然后调整填充,如下所示:
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

cropToPadding选项允许您进行解决方法,以防边距无法正常工作。

希望这可以帮到您。


0
边距有时候不起作用。
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width = 100;
params.height = 100;
params.setMargins( 0,0,0,0 );
imageView.setLayoutParams(params);

边距工作中:
final ImageView imageView = ImageView imageView = (ImageView) findViewById(R.id.imageView1);

imageView.post(() -> {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        params.width = 100;
        params.height = 100;
        params.setMargins( 0,0,0,0 );
        imageView.setLayoutParams(params);
    });

一些布局参数必须在程序中设置ImageView之前设置。例如,在"onCreate"中设置太早了。我怀疑它被覆盖了,但不确定。
所以你必须稍后设置它,例如使用"ImageView.post()"监听器,在绘制之后调用。第一个代码片段有时太早了,第二个可以工作。
顺便问一下,为什么要使用"findViewRoot"来获取ImageView?直接使用"findViewById"不就可以了吗?

根据目前的写法,你的回答不够清晰。请编辑以添加更多细节,帮助其他人理解这如何回答所提出的问题。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined
对不起,机器先生,我对您的母语不太擅长,所以我回答得很简短。谢谢您的指正,希望现在好一些了。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接