The use of Setlayoutparams
Layoutparams inherits from Android.View.ViewGroup.LayoutParams.
Layoutparams equivalent to a layout of the information collection, which encapsulates the layout of the location, height, width and other information. Suppose that an area on the screen is occupied by a layout, and if a view is added to a layout, it is best to tell the user how it is intended to be laid out, that is, to pass in an approved layoutparams.
但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值: 1,一个确定的值; 2,FILL_PARENT,即填满(和父容器一样大小); 3,WRAP_CONTENT,即包裹住组件就好。
Layouts that are dynamically built in Java code are often written like this:
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
The above sentence is actually a child to the father, that is, the parent layout under the child control to set this sentence.
Because the layout is many, although all inherits to the ViewGroup but each layout still has the very big difference.
It is clear that the above sentence should be written in such a way as to be accurate:
setLayoutParams(newTableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));
This means that the parent layout of this child control is a TableRow, so there are too many layoutparams, so it should be clearly specified.
Below are the next two common to the layout:
Framelayout the dynamic setting of the child control Center, the dynamic use of Java code to achieve this:
FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT);.gravity = Gravity.CENTER;btn.setLayoutParams(lytp);
Relativelayout the center of the child control under Dynamic settings:
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);
Setlayoutparams Error
textView.setLayoutParams(new TextSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Why use Textswitcher's layoutparams?
Check the API, you can see such a sentence these supply parameters to the parent of this view specifying how it should be arranged.
This means that you must use the parent control's layoutparams. If the parent control is linearlayout, then of course it must be written linearlayout.layoutparams
Layoutparams A class that has more than one control, the key to referencing which class is to see what kind of view control the parent control is.
Android Setlayoutparams FAQ Summary