With regard to the common Android layouts, there are already a number of examples of XML file implementations. But how do you use code to implement it? Of course, the use of code implementation is not much necessary, but also not advocated, but I think the use of code to achieve these layouts, you can better understand the SDK API, so here also tidy up some, and share with you.
First look at the API inheritance diagram for the class that the layout corresponds to:
Android Common layout code implementation all layouts will correspond to the related classes, which are inherited from the Android.view.ViewGroup class. And Linearlayout,relativelayout are in the Android.widget bag. In addition, Tablelayout is inherited from LinearLayout.
The following code is directly attached.
Use code to set the linear layout
private void Setlinearlayout () {
LinearLayout llayout = new LinearLayout (this);
Llayout.setorientation (linearlayout.vertical); Set the alignment of the linear layout
TextView TextView = new TextView (this);
Textview.settext ("Linear layout of code implementation");
Textview.settextcolor (color.red);
Textview.setgravity (Gravity.center); Sets the alignment of the text content
Linearlayout.layoutparams Ll_lpara = new Linearlayout.layoutparams (MP,WC); ll_lpara.gravity = gravity.center_horizontal;//Set the alignment of the control in the layout
Llayout.addview (Textview,ll_lpara);
Button btn = New button (this);
Btn.settext ("button");
Llayout.addview (Btn,ll_lpara); Adds a control
Setcontentview (llayout) by the specified property;
To implement the effect diagram:
=========================================================================
Use code to set relative layout private void Setrelativelayout () {relativelayout rlayout = new Relativelayout (this); Rlayout.setpadding (10, 10, 10, 10);
Unit: pixels int textviewid = 100;
TextView TextView = new TextView (this);
Textview.setid (TEXTVIEWID);
Textview.settext ("Please enter:");
Relativelayout.layoutparams rl_lpara1 = new Relativelayout.layoutparams (MP, WC);
Rlayout.addview (TextView, RL_LPARA1);
int edittextid = 200;
EditText edittext = new EditText (this);
Edittext.setid (Edittextid); Edittext.setbackgroundresource (Android. R.drawable.editbox_background);
Set background, with ANDROID:BACKGROUMD relativelayout.layoutparams rl_lpara2 = new Relativelayout.layoutparams (MP, WC); Rl_lpara2.addrule (RELATIVELAYOUT.BELOW,TEXTVIEWID);
Sets the relative properties, first specifying the ID rlayout.addview (edittext, rl_lpara2) of the relative control;
int backbtnid = 300;
Button backbtn = New button (this);
Backbtn.setid (Backbtnid);
Backbtn.settext ("return");
Relativelayout.layoutparams rl_lpara3 = new Relativelayout.layoutparams (WC, WC); Rl_lpara3.addrule (Relativelayout.below, Edittextid); Rl_lpara3.addrule (Relativelayout.align_parent_right);
Sets the relative properties of the parent control Rlayout.addview (BACKBTN, RL_LPARA3);
Button okbtn = New button (this);
Okbtn.settext ("OK");
Relativelayout.layoutparams rl_lpara4 = new Relativelayout.layoutparams (WC, WC);
Rl_lpara4.addrule (relativelayout.left_of, Backbtnid);
Rl_lpara4.addrule (Relativelayout.align_top,backbtnid);
Rlayout.addview (OKBTN, RL_LPARA4);
Setcontentview (rlayout); }
To implement the effect diagram:
=========================================================================
Set table layout with code private void Settablelayout () {tablelayout tlayout = new Tablelayout (this); Tlayout.setcolumnstretchable (2, true);
Lengthen the index from the 2nd column starting from 0 tablelayout.layoutparams Tl_lpara = new Tablelayout.layoutparams (MP,WC); 1. TableRow do not need to set layout_width, Layout_height//2.
The control in TableRow cannot set the Layout_span property TableRow TR1 = new TableRow (this);
TextView textView0 = new TextView (this);
Textview0.settext ("No. 0 column");
Tr1.addview (TEXTVIEW0);
TextView textView1 = new TextView (this);
Textview1.settext ("1th column");
Tr1.addview (TEXTVIEW1);
TextView textView2 = new TextView (this);
Textview2.settext ("2nd column");
Textview2.setbackgroundcolor (Color.cyan);
Tr1.addview (TEXTVIEW2);
Tlayout.addview (TR1, Tl_lpara);
TableRow TR2 = new TableRow (this);
Button btn0 = New button (this);
Btn0.settext ("button 0");
Tr2.addview (Btn0);
Button btn1 = New button (this);
Btn1.settext ("button 1");
Tr2.addview (BTN1);
Button btn2 = New button (this);
Btn2.settext ("button 2"); Tr2.addvieW (BTN2);
Button btn3 = New button (this);
Btn3.settext ("button 3");
Tr2.addview (BTN3);
Tlayout.addview (TR2, Tl_lpara);
Setcontentview (tlayout); }
To implement the effect diagram: