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