Android code to write controls instead of XML simple examples

A simple exercise of a button control.

Implementation code:

button btn=new button (handlertoactivity.this);
linearlayout.layoutparams params=new layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);
btn.setlayoutparams (params);
btn.setlayoutparams (params);
btn.settextcolor (color.white);
btn.setpadding (0, 50, 0, 2);
btn.setgravity (gravity.center_horizontal);
btn.settextsize (14);
btn.settext ("This is the button");
btn.setbackgroundresource (r.drawable.alertdialog_button);
buttonlayout.addview (btn);

Things to note:

1, define the layoutparams, some properties are set directly through the button control,Some are in params, such as gravity, margin, etc.

2. Also define a layout to receive.

  • Previous Summary of four methods for Android to parse XML files
  • Next Android status bar customization and modification methods