在java代碼中設置margin

咱們日常能夠直接在xml裏設置margin,如:html

<ImageView android:layout_margin="5dip" android:src="@drawable/image" />

可是有些狀況下,須要在java代碼裏來寫,但是View自己沒有setMargin方法,怎麼辦呢?java

 

經過查閱android api,咱們發現android.view.ViewGroup.MarginLayoutParams有個方法setMargins(left, top, right, bottom).android

其直接的子類有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams.api

 

使用方法:佈局

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 20, 30, 40);
imageView.setLayoutParams(lp);

 原文:http://greatverve.cnblogs.com/archive/2012/01/29/android-margin.htmlthis

android 使用代碼實現 RelativeLayout佈局

 

 RelativeLayout rl = new RelativeLayout(this);
        
        Button btn1 = new Button(this);
        btn1.setText("----------------------");
        btn1.setId(1);
        
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        // btn1 位於父 View 的頂部,在父 View 中水平居中
        rl.addView(btn1, lp1 );
       
        Button btn2 = new Button(this);
        btn2.setText("|\n|\n|\n|\n|\n|");
        btn2.setId(2);
       
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp2.addRule(RelativeLayout.BELOW, 1);
        lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);
        // btn2 位於 btn1 的下方、其左邊和 btn1 的左邊對齊
        rl.addView(btn2, lp2);
       
        Button btn3 = new Button(this);
        btn3.setText("|\n|\n|\n|\n|\n|");
        btn3.setId(3);
       
        RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       lp3.addRule(RelativeLayout.BELOW, 1);
        lp3.addRule(RelativeLayout.RIGHT_OF, 2);
        lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);
        // btn3 位於 btn1 的下方、btn2 的右方且其右邊和 btn1 的右邊對齊(要擴充)
        rl.addView(btn3,lp3);
       
        Button btn4 = new Button(this);
        btn4.setText("--------------------------------------------");
        btn4.setId(4);
       
        RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp4.addRule(RelativeLayout.BELOW, 2);
        lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        // btn4 位於 btn2 的下方,在父 Veiw 中水平居中
        rl.addView(btn4,lp4);
       
       
        setContentView(rl);

原文:http://kukuqiu.iteye.com/blog/1018396spa

動態修改RelativeLayout的寬高:.net

參數能夠.屬性設置,但數值是像素,須要轉化爲dp單位。code

RelativeLayout.LayoutParams linearParams =  (RelativeLayout.LayoutParams)rela_addnote_notetype.getLayoutParams();  
        linearParams.height = 44;  
        rela_addnote_notetype.setLayoutParams(linearParams);  

原文:http://blog.csdn.net/chenguang79/article/details/37874793xml

 Android適配所需知識點LayoutParams:

http://www.android100.org/html/201406/05/18971.html

相關文章
相關標籤/搜索