簡體   English   中英

以編程方式更改線性布局上邊距android

[英]Change linear layout top margin programmatically android

我在一幀布局中有兩個線性布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="left"
    android:orientation="vertical">

     <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/image12">
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">   

                <LinearLayout
                    android:id="@+id/layoutbtnlinear_aboutme"
                    android:layout_width="fill_parent"
                    android:layout_height="55dp"
                    android:gravity="bottom"
                    android:layout_marginTop="10dp"
                     android:background="#b2b2b2" 
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/imgShare_layout_aboutme"
                        android:layout_width="wrap_content"
                        android:layout_height="55dp"
                        android:layout_gravity="right|center|end"
                        android:layout_weight="1.63"
                        android:src="@drawable/ic_share" />

                    <TextView
                        android:id="@+id/txtTitle_layout_aboutme"
                        android:layout_width="wrap_content"
                        android:layout_height="55dp"
                        android:layout_gravity="left"
                        android:layout_weight="0.3"
                        android:fontFamily="Times New Roman"
                        android:text="About Me"
                        android:textColor="@android:color/black"
                        android:textSize="35sp"
                        android:textStyle="italic" />
                </LinearLayout>

            <LinearLayout
                android:id="@+id/content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <ImageButton
                       android:id="@+id/btnSlidingDrawerHandler_layout_aboutme"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center"
                       android:background="@drawable/ic_1_navigation_collapse" />


               <ListView
                    android:id="@+id/listView_layout_aboutme"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:footerDividersEnabled="true"
                    android:dividerHeight="4px"
                    android:isScrollContainer="true" 
                    android:scrollbarAlwaysDrawVerticalTrack="true" 
                    android:scrollbarStyle="outsideInset" 
                    android:scrollbars="vertical">
                </ListView>
            </LinearLayout>
        </LinearLayout>         
     </FrameLayout>  

</LinearLayout>

在這里,我將 id layoutbtnlinear_aboutme的線性布局的top margin layoutbtnlinear_aboutme為 10dp 但在代碼中我想在某些情況下將此 10dp 更改為 50dp 如何以編程方式更改此上邊距?

   layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme);
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();
   params.setMargins(0, 50, 0, 0); 
   layout.setLayoutParams(params);

由於它們的父布局,LayaoutParams 通常會在設置邊距時造成混亂......所以這個 MarginLayoutParams 非常有用,它適用於所有布局。

Java代碼

MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.width = 200; //Ths value 200 is in px... Please convert in DP
params.leftMargin = 100; 
params.topMargin = 200;

科特林代碼

val params: MarginLayoutParams = view!!.layoutParams as MarginLayoutParams
params.width = 200 
params.leftMargin = 100 
params.topMargin = 200

這會更新頂部邊距,而無需更新其他邊距值。

LinearLayout layout = (LinearLayout) findViewById(R.id.your_linear_layout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.topMargin = 200;
layout.setLayoutParams(layoutParams);

用這個

    layout = (LinearLayout) findViewById(R.id.layuout);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.setMargins(30, 20, 30, 0);
layout.setLayoutParams(layoutParams );

使用此方法在 dp 中設置邊距

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        final float scale = getBaseContext().getResources().getDisplayMetrics().density;
        // convert the DP into pixel
        int l =  (int)(left * scale + 0.5f);
        int r =  (int)(right * scale + 0.5f);
        int t =  (int)(top * scale + 0.5f);
        int b =  (int)(bottom * scale + 0.5f);

        p.setMargins(l, t, r, b);
        view.requestLayout();
    }
}

調用方法:

setMargins(linearLayout,0,0,5,0);

https://stackoverflow.com/a/50951911/8523391

科特林解決方案:

fun LinearLayout.setMargins(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
    val params: LinearLayout.LayoutParams = this.layoutParams as LinearLayout.LayoutParams

    val left = left?.let { it } ?: params.leftMargin
    val top = top?.let { it } ?: params.topMargin
    val right = right?.let { it } ?: params.rightMargin
    val bottom = bottom?.let { it } ?: params.bottomMargin

    params.setMargins(left, top, right, bottom)
    this.requestLayout()
}

那么你只需要:

layoutbtnlinear_aboutme.setMargins(top = 10)

我直接使用下面的代碼設置了邊距(我嘗試使用 LinearLayout.LayoutParams 但對我不起作用)

LinearLayout layout = (LinearLayout)findViewById(R.id.yourrelative_layout);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(3, 300, 3, 3); 
layout.setLayoutParams(params);

這里只是要注意 LayoutParams 應該為以下包android.widget.RelativeLayout.LayoutParams導入,除非你會遇到錯誤。

暫無
暫無

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

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