Android--addview () and Setview ()

Source: Internet
Author: User

There are two ways to add a view file, one is to define layout in an XML file, and the other is to generate the layouts file dynamically in Java code.

Layout defined in XML to be converted to view requires the use of the Layoutinflater class.

1. Constructing an XML file

2.LayoutInflater

When it comes to AddView, first look at the Layoutinflater class. The most important feature of this class is the ability to convert the layout of XML representations into view. For the sake of understanding, we can compare it to Findviewbyid (), both of which instantiate an object, and the difference is that Findviewbyid () is instantiated for the specific widget control under the XML layout file, and Layoutinflater is looking for res/ The XML layout file under layout/is instantiated.

(1) Create

Layoutinflater inflater= (Layoutinflater) Context.getsystemservice (Context.layout_inflater_service);

Layoutinflater inflater = Layoutinflater.from (activity.this);

Layoutinflater inflater = Getlayoutinflater ();

The three methods are essentially the same.

(2) Inflate ()

Use Layoutinflater.inflate () to convert the layout file to view.

View view = Inflater.inflate (R.layout.login, NULL);

3. Add a View file

For example, if you define a toast, you can set the view file

Toast.setview (view);

====

Now give an example of a common toast toast--let the bread with pictures and text appear in the center, see the code:

Where the main file is only placed a Button,xml file will not be mentioned.

Android--addview () and Setview ()

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.