Understanding of LayoutParams setting parameters in android

Reprinted from: https://www.cnblogs.com/hubing/p/5104110.html

There is such an application scenario: there is a linearLayout control in which a textView control is laid out

First, create a linear layout object

LinearLayout layout = new LinearLayout(this);//为本Activity创建一个线性布局对象
//并且设置它的属性 android:layout_width 与 android:layout_height 都为 FILL_PARENT
//布局方面的属性设置方式

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

Then, create a TextView for this Activity, the code is as follows

TextView textView = new TextView(this);
//然后设置TextView的属性
textView.setText(R.string.hello);
textView.setId(34);

For layout attributes, set it like this

 LinearLayout.LayoutParams textviewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

Then add this TextView to the linear layout object

layout.addView(textView,textviewParams);//加入的同时,也就设置了TextView相对于布局对象的布局属性 android:layout_width 与 android:layout_height

The last step is to set the top-level interface of this Activity to a linear layout

setContentView(layout,layoutParams); //同时也就设置了布局对象的android:layout_width 与 android:layout_height

Personal guess: the properties of the control are divided into the control's own characteristic properties and layout properties: the relationship property with the parent control.

 

Understanding and application of LayoutParams:

LayoutParams is equivalent to a Layout information package, which encapsulates the position, height, width and other information of the Layout. Assuming that an area on the screen is occupied by a Layout, if a View is added to a Layout, it is best to tell the Layout the user's desired layout method, that is, to pass in a recognized layoutParams.
You can describe LayoutParams like this. On the chess board, each piece occupies a position, that is, each piece has a position information. For example, the piece is in 4 rows and 4 columns. Here, "4 rows and 4 columns" means The LayoutParams of the chess pieces.

But the LayoutParams class simply describes the width and height. Both width and height can be set to three values:
       1. A certain value;
       2. FILL_PARENT, which is filled (the same size as the parent container);
       3. WRAP_CONTENT, which is Just wrap the components.

 

The layout dynamically constructed in JAVA is often written like this:

setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

The above sentence is actually child to parent, that is to say, the child control under the parent layout should set this sentence.

Because there are many layouts, although they all inherit from ViewGroup, the layouts are still very different.

Obviously the above sentence should be written like this to be accurate:

setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT));

Here are two commonly used layouts:

1. Dynamically set the child control to be centered under FrameLayout, and dynamically use JAVA code to achieve this:

FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80, LayoutParams.WRAP_CONTENT);
lytp.gravity = Gravity.CENTER;
btn.setLayoutParams(lytp);

2. Dynamically set the center of the child control under 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);

 

Guess you like

Origin blog.csdn.net/qq_37381177/article/details/111650579