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 ()