In some specific situations, you cannot use static methods to define the view used in the page. For example, a custom graph is an independent view. To insert it into a page as a part, you need the following methods:
Linearlayout L = new linearlayout (this); // L indicates the current page layout.
L. addview (myview); // Add a new view
L. setpadding (20,390, 20, 40); // set the position
Linearlayout. layoutparams P = new linearlayout. layoutparams (linearlayout. layoutparams. wrap_content, linearlayout. layoutparams. wrap_content );
L. setlayoutparams (p); // parameters of the new view
This. addcontentview (L, P); // Add a new view
<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 describes two commonly used la s:
1. In framelayout, the Child control is set to center dynamically, and the Java code should be implemented 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);