I have two imageView in LinearLayout and I want to set margin programmatically,but problem is that margin apply only on one imageView. Here is my .xml file..,
<?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..,
@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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.