简体   繁体   English

以编程方式将边距设置为多个ImageView

[英]Set Margin to Multiple ImageView Programmatically

I have two imageView in LinearLayout and I want to set margin programmatically,but problem is that margin apply only on one imageView. 我在LinearLayout中有两个imageView,我想以编程方式设置边距,但是问题是,边距仅适用于一个imageView。 Here is my .xml file.., 这是我的.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_margin="@dimen/ivMarginBottom"
    android:layout_height="match_parent"
    android:background="@drawable/image_round"
    android:orientation="vertical"
     >

   <ImageView
        android:id="@+id/imageOne"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/ivMarginBottom"
        android:layout_weight="1"
        android:background="@color/black"
        android:src="@drawable/icongallery" 
        />

    <ImageView 
        android:id="@+id/imageTwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/black"
        android:src="@drawable/icongallery" />

</LinearLayout>

and here is my .java code.., 这是我的.java代码。

@Override
    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        int left_margin = progress;
        int top_margin = progress;
        int right_margin = progress;
        int bottom_margin = progress;

        MarginLayoutParams marginParams1 = new MarginLayoutParams(imageOne.getLayoutParams());
        marginParams1.setMargins(left_margin, top_margin, right_margin, bottom_margin);
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(marginParams1);
        imageOne.setLayoutParams(layoutParams1);

        MarginLayoutParams marginParams2 = new MarginLayoutParams(imageTwo.getLayoutParams());
        marginParams2.setMargins(left_margin, top_margin, right_margin, bottom_margin);
        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(marginParams2);
        imageTwo.setLayoutParams(layoutParams2);
}

first get Layout params like this 首先获得这样的布局参数

LinearLayout.LayoutParams lp =
                (LinearLayout.LayoutParams) imageView.getLayoutParams();

then set margins 然后设置边距

Checkout the following code: 签出以下代码:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(left_margin, top_margin, right_margin, bottom_margin);
    imageOne.setLayoutParams(lp);
    imageTwo.setLayoutParams(lp);

Here is your solution: 这是您的解决方案:

int left_margin = progress;
int top_margin = progress;
int right_margin = progress;
int bottom_margin = progress;

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1);
layoutParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
imageOne.setLayoutParams(layoutParams);
imageTwo.setLayoutParams(layoutParams);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM