Android uses the layoutParams code to dynamically layout the Space Location

Source: Internet
Author: User

Android uses the layoutParams code to dynamically layout the Space Location

 

 

Android development: LayoutParams usage

LayoutParams inherits from Android. View. ViewGroup. LayoutParams.
LayoutParams is equivalent to a Layout information package, which encapsulates the location, height, width, and other information of Layout. Assume that a Layout occupies an area on the screen. if you add a View to a Layout, it is best to tell the Layout user the expected Layout method, that is, passing an accepted layoutParams.
It can be described as LayoutParams in this way. On the chess board, each piece occupies a position, that is, each piece has a position information. For example, this piece has four rows and four columns, here, "four rows and four columns" are the LayoutParams of the pawns.


However, the LayoutParams class simply describes the width and height. You can set the width and height to three values:
1. A definite value;
2, FILL_PARENT, that is, fill (the same size as the parent container );
3. WRAP_CONTENT: Wrap the component.


The layout dynamically built in JAVA is often written as follows:

 

setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
The above statement is actually a sub-parent, that is, the sub-control in the parent layout should be set. Because there are many la s, although they all inherit from ViewGroup, The la s are quite different. Obviously, the above sentence should be written as follows:
setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));
This indicates that the parent layout of the Child control is a TableRow, so there are too many LayoutParams, so it should be clearly specified. The following are two commonly used la S: 1. in FrameLayout, the Child control is set to center dynamically and implemented in JAVA code as follows: This indicates that the parent layout of the Child control is a TableRow, and there are too many LayoutParams, so it should be specified explicitly. The following are two common la S: 1. In FrameLayout, the Child control is set to center dynamically, and the JAVA code is used as follows:
FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);lytp .gravity = Gravity.CENTER;btn.setLayoutParams(lytp);
2. dynamically set the child control center in RelativeLayout:
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); btn1.setLayoutParams(lp);

 

Instance:

Obtain and parse the Json object from the webpage response

 

@SuppressWarnings(unchecked)Map
 
   markJsonMap = (Map
  
   ) mapJson.get(adlogo);if(markJsonMap != null){String position = (String) markJsonMap.get(position);String markTumbString = (String) markJsonMap.get(thumb);String smlscreen = (String) markJsonMap.get(smlscreen);String bigscreen = (String) markJsonMap.get(bigscreen);HomeActivity.mCornerMark.setPosition(position);HomeActivity.mCornerMark.setThumb(markTumbString);HomeActivity.mCornerMark.setSmlscreen(smlscreen);HomeActivity.mCornerMark.setBigscreen(bigscreen);setCornerMark();}
  
 

After parsing, the Code sets the size and position of the badge image and asynchronously downloads the image.

 

 

/*** Dynamically set the badge position **/private void setCornerMark () {if (! HomeActivity. mCornerMark. getThumb (). equals () & HomeActivity. mCornerMark. getThumb (). length ()! = 0) {// WindowManager wm = (WindowManager) mContex //. getSystemService (Context. WINDOW_SERVICE); // int width = wm. getdefadisplay display (). getWidth (); // int height = wm. getdefadisplay display (). getHeight (); FrameLayout. layoutParams reParams = (android. widget. frameLayout. layoutParams) HomeActivity. home_surface_viewLyout.getLayoutParams (); int width = reParams. width; int height = reParams. height; RelativeLayout. layoutParams params; // width, height * 48/128 // obtain the control layout parameter Configuration config = mContex. getResources (). getConfiguration (); if (config. orientation = Configuration. ORIENTATION_LANDSCAPE) {String [] paramString = HomeActivity. mCornerMark. getBigscreen (). split (,); int whidthString = Integer. parseInt (paramString [0]); int heightString = Integer. parseInt (paramString [1]); // convert dpwhidthString = DensityUtil. px2dip (mContex, whidthString); heightString = DensityUtil. px2dip (mContex, heightString); params = new LayoutParams (whidthString, heightString);} else {String [] paramString = HomeActivity. mCornerMark. getSmlscreen (). split (,); int whidthString = Integer. parseInt (paramString [0]); int heightString = Integer. parseInt (paramString [1]); // convert dpwhidthString = (int) (width * 0.005 * whidthString); heightString = (int) (height * 0.005 * heightString ); whidthString = DensityUtil. px2dip (mContex, whidthString); heightString = DensityUtil. px2dip (mContex, heightString); params = new RelativeLayout. layoutParams (whidthString, heightString);} // dynamically specify the control size and location Log. v (Position, HomeActivity. mCornerMark. getPosition (); if (HomeActivity. mCornerMark. getPosition (). equals (1) {params. leftMargin = 20; params. topMargin = 20; params. addRule (RelativeLayout. ALIGN_PARENT_LEFT, RelativeLayout. TRUE); params. addRule (RelativeLayout. ALIGN_PARENT_TOP, RelativeLayout. TRUE);} else if (HomeActivity. mCornerMark. getPosition (). equals (2) {params. rightMargin = 20; params. topMargin = 20; params. addRule (RelativeLayout. ALIGN_PARENT_RIGHT, RelativeLayout. TRUE); params. addRule (RelativeLayout. ALIGN_PARENT_TOP, RelativeLayout. TRUE);} else if (HomeActivity. mCornerMark. getPosition (). equals (3) {params. leftMargin = 20; params. bottomMargin = 20; params. addRule (RelativeLayout. ALIGN_PARENT_LEFT, RelativeLayout. TRUE); params. addRule (RelativeLayout. ALIGN_PARENT_BOTTOM, RelativeLayout. TRUE);} else if (HomeActivity. mCornerMark. getPosition (). equals (4) {params. rightMargin = 20; params. bottomMargin = 20; params. addRule (RelativeLayout. ALIGN_PARENT_RIGHT, RelativeLayout. TRUE); params. addRule (RelativeLayout. ALIGN_PARENT_BOTTOM, RelativeLayout. TRUE);} HomeActivity. img_cornermark.setLayoutParams (params); DownLoadTask downLoadTask = new downloadtask(homeactivity.img_cornermark1_1_downloadtask.exe cute (HomeActivity. mCornerMark. getThumb ());}}

Problems:

 

1. What is the layout of RelativeLayout?

The hierarchical layout of RelativeLayout is based on the xml file Relativelayout from the underlying layer to the outer layer.
2. In RelativeLayout, the code implementation location is
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

 

3. Different cell phone Resolutions and Other column display:

 

String [] paramString = HomeActivity. mCornerMark. getSmlscreen (). split (,); int whidthString = Integer. parseInt (paramString [0]); int heightString = Integer. parseInt (paramString [1]); // convert dpwhidthString = (int) (width * 0.005 * whidthString); heightString = (int) (height * 0.005 * heightString ); whidthString = DensityUtil. px2dip (mContex, whidthString); heightString = DensityUtil. px2dip (mContex, heightString); params = new RelativeLayout. layoutParams (whidthString, heightString );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.