簡體   English   中英

Android:如何以編程方式設置水平滾動視圖圖像邊距

[英]Android: How to set horizontal scroll view image margins programmatically

我正在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.

使用此功能設置運行時余量以查看

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM