我在LinearLayout中有两个imageView,我想以编程方式设置边距,但问题是边距仅适用于一个imageView。这是我的.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>
这是我的.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);
}
答案 0 :(得分:0)
首先获得像这样的布局参数
LinearLayout.LayoutParams lp =
(LinearLayout.LayoutParams) imageView.getLayoutParams();
然后设置边距
答案 1 :(得分:0)
检查以下代码:
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);
答案 2 :(得分:0)
以下是您的解决方案:
void