Code layout for Android programming (1)
Preface:
I am used to writing layout with XML and writing layout with code. I am afraid it will be very similar. But after all, sometimes we still need to use code to write the layout.
Code layout has many similarities with XML layout, which can be used directly in most methods. Only a few methods do not have the same writing style. Next, I will introduce several commonly used controls in several articles.
Take main. xml of the android project as an example to see how the code is implemented.
1. Set the current layout
This issue is only for junior level personnel. Others may skip this issue.
We know that when we create an activity, the program will help us build it:
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}
Setcontentview (R. layout. Main); is used to set the style of the current layout.
If we use code to write the layout, We need to input a view object here. In fact, it is linear layout, relative layout, text box and other objects. For example, if we create a linear layout, We will upload it here, for example:
LinearLayout mLinearLayout = new LinearLayout(this);setContentView(mLinearLayout);
Ii. Linear Layout
Linear layout is often used. We should familiarize ourselves with the code layout first.
// Create the linearlayout object linearlayout mlinearlayout = new linearlayout (this); // create the layout style width and height, corresponding to the XML layout: // Android: layout_width = "fill_parent" // Android: layout_height = "fill_parent" mlinearlayout. setlayoutparams (New linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); // set the direction, corresponding to the XML layout: // Android: Orientation = "vertical" mlinearlayout. setorientation (linearlayout. vertical );
There are many layoutparams. If you are not familiar with laparparams, you 'd better add linearlayout in front to facilitate your identification.
In this way, we will create an outer layout in the simplest way. Next, we will implement textview.
3. textview
// Create textview object textview mtextview = new textview (this); // set the text mtextview. settext ("Hello World"); // create a layout style for the layout, corresponding to the XML layout: // Android: layout_width = "fill_parent" // Android: layout_height = "wrap_content" linearlayout. layoutparams mlayoutparams = new linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); // Add it to the parent class layout and the layout style mlinearlayout. addview (mtextview, mlayoutparams );
The layout style set for it is different from the parent class. You can understand that the layout style set for it is not set for you, but to tell the parent class, where should I place it. Understanding this is critical because it is the key to setting the component location!
It also has many other parameter settings:
Mtextview. settextcolor (-1); // font color
Mtextview. settextsize (16); // font size
Finally, if the code is correct, it runs the same way as running XML.
The full version code is as follows:
Public class testlayout extends baseactivity {@ overrideprotected void initrecourse () {// todo auto-generated method stub} @ overrideprotected void initdata () {// todo auto-generated method stub} @ overrideprotected viewgroup initview () {// create the linearlayout object linearlayout mlinearlayout = new linearlayout (this); // create a layout style width and height, in the XML layout, // Android: layout_width = "fill_parent" // Android: layout_height = "fill_parent" mlinearlayout. setlayoutparams (New linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); // set the direction, corresponding to the XML layout: // Android: Orientation = "vertical" mlinearlayout. setorientation (linearlayout. vertical); // create textview object textview mtextview = new textview (this); // set text mtextview. settext ("Hello World"); // create a layout style for the layout, corresponding to the XML layout: // Android: layout_width = "fill_parent" // Android: layout_height = "wrap_content" linearlayout. layoutparams mlayoutparams = new linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); // Add it to the parent class layout and the layout style mlinearlayout. addview (mtextview, mlayoutparams); Return mlinearlayout ;}}
Appendix: rewritten Activity
Here, the rewrite activity aims to better compile code layout services for us. In future code examples, we will use the following inheritance method.
Public abstract class baseactivity extends activity {public handler;/** initialize data */protected abstract void initdata ();/** initialize Resources */protected abstract void initrecourse (); /** initialization interface */protected abstract view initview ();/** handle handler returned information */Public void dispatchmessage (Message MSG) {} protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setrequestedorientation (activityinfo. screen_orientation_portrait); initdata (); initrecourse (); setcontentview (initview (); handler = new handler () {public void dispatchmessage (Message MSG) {baseactivity. this. dispatchmessage (MSG );}};}}
Note: The resource loading here is generally loaded from the asset.
Preface:
I am used to writing layout with XML and writing layout with code. I am afraid it will be very similar. But after all, sometimes we still need to use code to write the layout.
Code layout has many similarities with XML layout, which can be used directly in most methods. Only a few methods do not have the same writing style. Next, I will introduce several commonly used controls in several articles.
Take main. xml of the android project as an example to see how the code is implemented.
1. Set the current layout
This issue is only for junior level personnel. Others may skip this issue.
We know that when we create an activity, the program will help us build it:
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}
Setcontentview (R. layout. Main); is used to set the style of the current layout.
If we use code to write the layout, We need to input a view object here. In fact, it is linear layout, relative layout, text box and other objects. For example, if we create a linear layout, We will upload it here, for example:
LinearLayout mLinearLayout = new LinearLayout(this);setContentView(mLinearLayout);
Ii. Linear Layout
Linear layout is often used. We should familiarize ourselves with the code layout first.
// Create the linearlayout object linearlayout mlinearlayout = new linearlayout (this); // create the layout style width and height, corresponding to the XML layout: // Android: layout_width = "fill_parent" // Android: layout_height = "fill_parent" mlinearlayout. setlayoutparams (New linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); // set the direction, corresponding to the XML layout: // Android: Orientation = "vertical" mlinearlayout. setorientation (linearlayout. vertical );
There are many layoutparams. If you are not familiar with laparparams, you 'd better add linearlayout in front to facilitate your identification.
In this way, we will create an outer layout in the simplest way. Next, we will implement textview.
3. textview
// Create textview object textview mtextview = new textview (this); // set the text mtextview. settext ("Hello World"); // create a layout style for the layout, corresponding to the XML layout: // Android: layout_width = "fill_parent" // Android: layout_height = "wrap_content" linearlayout. layoutparams mlayoutparams = new linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); // Add it to the parent class layout and the layout style mlinearlayout. addview (mtextview, mlayoutparams );
The layout style set for it is different from the parent class. You can understand that the layout style set for it is not set for you, but to tell the parent class, where should I place it. Understanding this is critical because it is the key to setting the component location!
It also has many other parameter settings:
Mtextview. settextcolor (-1); // font color
Mtextview. settextsize (16); // font size
Finally, if the code is correct, it runs the same way as running XML.
The full version code is as follows:
Public class testlayout extends baseactivity {@ overrideprotected void initrecourse () {// todo auto-generated method stub} @ overrideprotected void initdata () {// todo auto-generated method stub} @ overrideprotected viewgroup initview () {// create the linearlayout object linearlayout mlinearlayout = new linearlayout (this); // create a layout style width and height, in the XML layout, // Android: layout_width = "fill_parent" // Android: layout_height = "fill_parent" mlinearlayout. setlayoutparams (New linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); // set the direction, corresponding to the XML layout: // Android: Orientation = "vertical" mlinearlayout. setorientation (linearlayout. vertical); // create textview object textview mtextview = new textview (this); // set text mtextview. settext ("Hello World"); // create a layout style for the layout, corresponding to the XML layout: // Android: layout_width = "fill_parent" // Android: layout_height = "wrap_content" linearlayout. layoutparams mlayoutparams = new linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); // Add it to the parent class layout and the layout style mlinearlayout. addview (mtextview, mlayoutparams); Return mlinearlayout ;}}
Appendix: rewritten Activity
Here, the rewrite activity aims to better compile code layout services for us. In future code examples, we will use the following inheritance method.
Public abstract class baseactivity extends activity {public handler;/** initialize data */protected abstract void initdata ();/** initialize Resources */protected abstract void initrecourse (); /** initialization interface */protected abstract view initview ();/** handle handler returned information */Public void dispatchmessage (Message MSG) {} protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setrequestedorientation (activityinfo. screen_orientation_portrait); initdata (); initrecourse (); setcontentview (initview (); handler = new handler () {public void dispatchmessage (Message MSG) {baseactivity. this. dispatchmessage (MSG );}};}}
Note: The resource loading here is generally loaded from the asset.