Android development tips: dynamically create a UI

Source: Internet
Author: User

The basic UI interface of Android is usually defined in an xml file and displayed on the interface through the setContentView of the activity. This is the simplest way to build the Android UI. In fact, in order to implement a more complex and flexible UI interface, you often need to dynamically generate the UI interface, or even dynamically change the UI based on the user's click or configuration. This article introduces this technique.


Assume that the xml file name of the Android project is activity_main.xml, which is defined as follows:


<LinearLayout xmlns: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, you want to display this simple interface in three ways. Note: The following code is implemented in the onCreate () function of MainActivity ).


1) the first method is to load data directly through the traditional setContentView (R. layout. *)., That is:


setContentView(R.layout.activity_main);                                                           TextView text = (TextView)this.findViewById(R.id.DynamicText);text.setText("Hello World");


2) The second method is to indirectly load data through LayoutInflater., That is:


LayoutInflater mInflater = LayoutInflater.from(this);     View contentView  = mInflater.inflate(R.layout.activity_main,null);                                                                                                           TextView text = (TextView)contentView.findViewById(R.id.DynamicText);text.setText("Hello World");                                              setContentView(contentView);


Note:


LayoutInflater is equivalent to a "layout loader", which can be obtained from the system in three ways, such:


Method 1: LayoutInflater. from (this );


Method 2: (LayoutInflater) this. getSystemService (this. LAYOUT_INFLATER_SERVICE );


Method 3: this. getLayoutInflater ();


The inflate method of the object can be used to load the specified xml file and convert it to a View class object. The control objects in the xml file can be obtained through the findViewById method of the View object.


3) The third method is to manually create the UI.


Any tag in the xml file is defined by corresponding classes. Therefore, we can create the required UI purely and dynamically without using the xml file. The example is as follows:


LinearLayout layout = new LinearLayout(this);                                                                                                      TextView text = new TextView(this);text.setText("Hello World");text.setLayoutParams(new    ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));                                                                                                      layout.addView(text);                                                                                                      setContentView(layout);


The Android dynamic UI creation technique is mentioned here. In this example, the simplest examples are used for ease of understanding. Therefore, the advantages and usage of dynamic UI creation may not be seen, but it doesn't matter. master the basic skills first. In the subsequent articles, we will gradually apply these technologies to understand their real application scenarios.


Or that sentence, what is not clear, welcome to leave a message to discuss or send a letter to the lujun.hust@gmail.com exchange.


This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1256593

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.