Dynamic creation of Android research UI interface

Source: Internet
Author: User

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


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.

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.