In daily development, we often use XML directly to lay out the interface, but use this layout way directly to the layout die, not flexible for the display of page elements, such as according to request JSON data from the server dynamic display layout style, the amount, of course, the frequency of use is not too high, of course , for a qualified programmer, of course, to master any kind of skill that can be used in development.
This time we're going to do a simple landing page to learn how to lay out the code in Android and document the knowledge points involved.
let's look at the actual effect of the operation:
The actual code for this project is pasted first, followed by a detailed description:
/** * Use code for the layout of the login interface * @author Wangke/public class Codelayoutactivity extends Appcompatactivity {@Override
protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Create a outermost root layout linearlayout rootlayout = new LinearLayout (this);
Rootlayout.setbackgroundcolor (Color.cyan);
Set the linear layout of the neutron view in a vertical direction rootlayout.setorientation (linearlayout.vertical); Create a root bureau of layoutparams Linearlayout.layoutparams rootparams = new Linearlayout.layoutparams (viewgroup.layoutparams .
Match_parent, ViewGroup.LayoutParams.MATCH_PARENT); Set the margin value of the root Bureau rootparams.setmargins (densityutil.dip2px (this,5), densityutil.dip2px (this,5), densityutil.dip2px (
this,5), densityutil.dip2px (this,5));
rootparams.gravity = Gravity.center_horizontal;
Rootlayout.setlayoutparams (Rootparams);
Set the outermost root of the layout of the background color rootlayout.setbackgroundcolor (Color.cyan);
Set the currently created root bureau to the activity Setcontentview (rootlayout);
Create a imageview ImageView Ivico = new ImageView (this) to display the user's avatar;
Ivico.setimageresource (R.drawable.ico);
Adds the ImageView created to display the avatar to the outermost linear layout rootlayout.addview (Ivico); Rootlayout.addview (Ivico) needs to be written in front of you when you get the layout through the child view, and the reason is clear, and the current imageview is not yet added to the parent view if you directly pass Getlayoutparams
() Of course, the first null pointer exception (can view the source) linearlayout.layoutparams Ivparams = (linearlayout.layoutparams) ivico.getlayoutparams ();
Setting the ImageView ivparams.width = densityutil.dip2px (this,100f) for the display head;
Ivparams.height = densityutil.dip2px (this,100f);
Set the centered way (the child view is positioned horizontally in the parent view) ivparams.gravity = Gravity.center_horizontal;
Ivparams.setmargins (0,densityutil.dip2px (this,100), 0,0);
Ivico.setlayoutparams (Ivparams);
Create a horizontal, linear layout for placing account-related view linearlayout usercountlayout = new LinearLayout (this);
Usercountlayout.setorientation (linearlayout.horizontal);Adds the current created linear layout to the root view Rootlayout.addview (usercountlayout);
Displays the username TextView TextView tvshowuser = new TextView (this);
Tvshowuser.settext ("User name:");
Usercountlayout.addview (Tvshowuser);
Linearlayout.layoutparams usercountlayoutparams = (linearlayout.layoutparams) tvshowuser.getlayoutparams (); Usercountlayoutparams.setmargins (densityutil.dip2px (this,20), densityutil.dip2px (this,20), DensityUtil.dip2px (
this,20), densityutil.dip2px (this,20));
Create EditText edittext edtusercount = new EditText (this) for filling in user accounts;
Usercountlayout.addview (Edtusercount);
Edtusercount.sethint ("Please enter user name");
Linearlayout.layoutparams edtusercountlayoutparams = (linearlayout.layoutparams) edtusercount.getlayoutparams ();
Edtusercount.setwidth (0);
Edtusercountlayoutparams.weight = 1;
The gravity attribute of the parent view determines the placement rule of the child view edtusercountlayoutparams.gravity = gravity.center_vertical; View.setgravity sideThe method is equivalent to android:gravity= "center", which represents the qualification//edtusercount.setgravity (Gravity.center) of the content in the view;
Create a linear layout to hold the password-related view linearlayout pwdlayout = new LinearLayout (this);
Pwdlayout.setgravity (linearlayout.horizontal); Linearlayout.layoutparams pwdlayoutparams = new Linearlayout.layoutparams (LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Pwdlayout.setlayoutparams (Pwdlayoutparams);
Rootlayout.addview (pwdlayout);
TextView Display password text TextView tvshowpwd = new TextView (this);
Tvshowpwd.settext ("Password:");
Pwdlayout.addview (TVSHOWPWD);
Linearlayout.layoutparams tvshowpwdparams = (linearlayout.layoutparams) tvshowpwd.getlayoutparams (); Tvshowpwdparams.setmargins (densityutil.dip2px (this,20), densityutil.dip2px (this,20), DensityUtil.dip2px (this,20)
, densityutil.dip2px (this,20));
tvshowpwdparams.gravity = gravity.center_vertical;
Edttext for entering a passwordEditText edtpwd = new EditText (this);
Edtpwd.sethint ("Please enter the password");
Pwdlayout.addview (EDTPWD); Use the new method to get layoutparams linearlayout.layoutparams edtpwdparams = new Linearlayout.layoutparams (viewgroup.layout
Params.wrap_content, ViewGroup.LayoutParams.WRAP_CONTENT);
Set weights edtpwdparams.weight = 1;
edtpwdparams.width = 0;
Set the layout parameters to Edttext edtpwd.setlayoutparams (edtpwdparams);
Button button Btnlogin = New button (this) for login;
Btnlogin.settext ("Landing");
Rootlayout.addview (Btnlogin);
Linearlayout.layoutparams btnloginparams = (linearlayout.layoutparams) btnlogin.getlayoutparams ();
Set the size of the button btnloginparams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
Btnloginparams.height= LinearLayout.LayoutParams.WRAP_CONTENT;
btnloginparams.gravity = Gravity.center_horizontal; }
}