Android data filler LayoutInflater

Source: Internet
Author: User

Android data filler LayoutInflater
The LayoutInflater class is more practical in applications. It can be called a layout filler or a pump, which means to fill the layout file in the desired position, layoutInflater is used to find the xml layout file under res/layout/, and instantiate this XML file into a View, which is somewhat similar to findViewById (), but findViewById () is to find the specific widget controls (Button and TextView) under the xml layout file ). LayoutInflater is required for an interface that is not loaded or that wants to be dynamically loaded. inflate () to load; for a loaded interface, use Activiyt. findViewById () method to obtain the interface elements. LayoutInflater: LayoutInflater layoutInflater = getLayoutInflater (); LayoutInflater layoutInflater2 = LayoutInflater. from (this); LayoutInflater layoutInflater3 = (LayoutInflater) this. getSystemService (Context. LAYOUT_INFLATER_SERVICE); First getLayoutInflater method: public LayoutInflater getLayoutInflater () {return getWindow (). getLayoutInflater ();} Method 2: public static LayoutInflater f Rom (Context context) {LayoutInflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); if (LayoutInflater = null) {throw new AssertionError ("LayoutInflater not found. ");} return LayoutInflater;} the last two methods call getSystemService. The first method is ~ LayoutInflater has four methods to implement the LayOutInflater filling layout. The Demo uses the first method: resource: the id of the layout file to be loaded. You need to load the layout file to the Activity for operations. Root: inflate () will return a View object. If the root object is not null, the root object will be returned as the root View of the xml file. If it is null, the xml file itself is a root View: public View inflate (int resource, ViewGroup root) public View inflate (XmlPullParser parser, ViewGroup root) public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot) public View inflate (int resource, ViewGroup root, boolean attachToRoot ~ Directly write the processing of click events: defines an Item file: <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/test" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/inflate_txt" android: layout_width = "match_parent" android: layout_height = "match_parent" android: text = "test"/> </LinearLayout> Click Event Processing: LayoutInflater layoutInflater = getLayoutInflater (); View view = layoutInflater. inflate (R. layout. item, null); TextView text = (TextView) view. findViewById (R. id. inflate_txt); text. setText ("http://www.cnblogs.com/xiaofeixiang"); text. setTextSize (18); text. setTextColor (Color. RED); AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setView (view); AlertDialog alertDialog = builder. create (); alertDialog. show ();

Related Article

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.