Android Setlayoutparams FAQ Summary

Source: Internet
Author: User

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

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.