Home>

Everyone often sees the common layout of android using xml files in daily lifeBut everyone knows how to do it in code?Using code implementation can help us learn SDK APIs, so we will organize my daily implementation of some common layout code implementations for Android and share with you

Regarding android common layout,There are already many examples using xml file implementations.But how to use code?Of course, it is not necessary to implement it in code.It is also not recommended.But I feel that implementing these layouts using code,You can better understand the sdk api, so I will organize some here,Share with you.

Take a look first,The api inheritance diagram of the class corresponding to the layout:

Android common layout code to achieve all the layout will correspond to the relevant classes,These classes are inherited from the android.view.viewgroup class. The linearlayout and relativelayout are all in the android.widget package. In addition, tablelayout is inherited from linearlayout.

Paste the code directly below.

//Set linear layout using code
 private void setlinearlayout () {
  linearlayout llayout=new linearlayout (this);
  llayout.setorientation (linearlayout.vertical);//Set the arrangement of the linear layout
  textview textview=new textview (this);
  textview.settext ("Linear layout implemented by code");
  textview.settextcolor (color.red);
  textview.setgravity (gravity.center);//Set 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);//Add controls by specified properties
  setcontentview (llayout);
 }

Implementation effect diagram:

======================================================= ========================

//Set relative layout using code
 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, same as android:backgroumd
  relativelayout.layoutparams rl_lpara2=new relativelayout.layoutparams (mp, wc);
  rl_lpara2.addrule (relativelayout.below, textviewid);//set relative properties,Need to specify the id of the relative control first
  rlayout.addview (edittext, rl_lpara2);
  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);//Set the relative property to 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);
 }

Implementation effect diagram:

======================================================= ========================

//Set the table layout using code
 private void settablelayout () {
  tablelayout tlayout=new tablelayout (this);
  tlayout.setcolumnstretchable (2, true);//stretch the second column from 0
  tablelayout.layoutparams tl_lpara=new tablelayout.layoutparams (mp, wc);
  //1. tablerow does not need to set layout_width, layout_height
  //2. Controls in tablerow cannot set layout_span property
  tablerow tr1=new tablerow (this);
  textview textview0=new textview (this);
  textview0.settext ("Column 0");
  tr1.addview (textview0);
  textview textview1=new textview (this);
  textview1.settext ("Column 1");
  tr1.addview (textview1);
  textview textview2=new textview (this);
  textview2.settext ("Column 2");
  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);
 }

Implementation effect diagram:

  • Previous Learn JavaScript closures with me
  • Next Using JQuery FancyBox plugin to achieve picture display special effects