Android:如何以编程方式设置水平滚动视图图像边距

时间:2015-07-24 18:26:44

标签: java android horizontalscrollview

我在xml中创建HorizontalScrollView并以编程方式插入图像,但我的边距有问题。如何为HorizontalScrollView中的图像设置水平边距?以下是我的代码:

xml:

<HorizontalScrollView
            android:id="@+id/HorizontalScrollView"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="15dp">

            <LinearLayout
                android:id="@+id/innerLay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <!--Image Views here -->

            </LinearLayout>

</HorizontalScrollView>

java:

ImageView imageView = new ImageView(getActivity());
scrollLinearLayout.addView(imageView);
//set height and other image properties.

1 个答案:

答案 0 :(得分:0)

使用此功能设置运行时边距以查看

public static void setMargins (View v, int left, int top, int right, int bottom) {
    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        p.setMargins(left, top, right, bottom);
        v.requestLayout();
    }
}