Android's basic UI interface is typically defined in an XML file and then displayed on the interface through the activity's Setcontentview, which is the simplest way to build the Android UI. In fact, in order to achieve a more complex and more flexible UI interface, often need to dynamically generate UI interface, even depending on the user's click or configuration, dynamically change the UI, this article describes the technique. Monitoring of touch events for possible Android devices for events and processes, across processes
Suppose an XML file named Activity_main.xml for Android works, defined as follows:
12345678910111213141516171819 |
<LinearLayoutxmlns:Android="Http://schemas.android.com/apk/res/android" xmlns:Tools="Http://schemas.android.com/tools" Android:Layout_width="Match_parent" Android:Layout_height="Match_parent" Tools:Context=". Mainactivity "> <TextView Android:ID="@+id/dynamictext" Android:Layout_width="Wrap_content" Android:Layout_height="Wrap_content"/></LinearLayout> |
In Mainactivity, there are three ways to display this simple interface (note: The following code is implemented in the Mainactivity onCreate () function).
(1) The first way, directly through the traditional Setcontentview (r.layout.*) to load , namely:
1234567 |
Setcontentview(R.Layout.Activity_main); TextViewtext =(TextView) This.Findviewbyid(R.ID.Dynamictext);text.SetText("Hello World"); |
(2) The second way, through the layoutinflater to indirectly load , namely:
12345678910111213 |
LayoutinflaterMinflater =Layoutinflater. from( This); ViewContentview =Minflater.Inflate(R.Layout.Activity_main,NULL); TextViewtext =(TextView)Contentview.Findviewbyid(R.ID.Dynamictext);text.SetText("Hello World"); Setcontentview(Contentview); |
Note:
Layoutinflater is equivalent to a "layout loader", and there are three ways to get to the layout loader object from the system, such as:
Method One: Layoutinflater.from (this);
Method Two: (Layoutinflater) This.getsystemservice (this. Layout_inflater_service);
Method Three: This.getlayoutinflater ();
The object's Inflate method allows you to load the specified XML file into a view class object, which can be obtained through the View object's Findviewbyid method.
(3) A third way to create a UI interface purely by hand
Any label in the XML file is defined by a corresponding class, so we can completely create the required UI interface purely dynamically without using an XML file, as in the following example:
1234567891011121314151617 |
LinearLayoutLayout =New LinearLayout( This); TextViewtext =New TextView( This);text.SetText("Hello World");text.Setlayoutparams(New ViewGroup.Layoutparams(Layoutparams.wrap_content,Layoutparams.wrap_content)); Layout.AddView(text); Setcontentview(Layout); |
The technique of
Android dynamic UI creation is here, in this example, in order to facilitate understanding, is the simplest example, so you may not see the advantages and purposes of dynamic UI creation, but it does not matter, first grasp the basic skills, the later article, will slowly apply these techniques, You will be able to understand the real application scenario.