Dynamic UI creation for Android research details, and androidui details

Source: Internet
Author: User

Dynamic UI creation for Android research details, and androidui details
Zookeeper

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. Monitors the touch events of Android devices and processes.

Assume that the xml file name of the Android project is activity_main.xml, which is 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 methods to display this simple interface (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:


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

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


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", 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:


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 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.


How to dynamically update the UI of a service in Android

Then, how can we dynamically update the UI of the service? Case: send a request to the remote server through the service and dynamically update the UI of the main program based on the results returned by the server. The main program can close or restart the service in real time. [Kunming Dana android training] We have previously introduced you to Android's UI design and background thread interaction. According to Android API introduction, services are generally run in the background and there is no interface. Then, how can we dynamically update the UI of the service? Case: send a request to the remote server through the service and dynamically update the UI of the main program based on the results returned by the server. The main program can close or restart the service in real time. [Where is the Kunming android training institution?] register BroadcastReceiver to register a BroadcastReceiver in the main program activity to receive broadcasts published by the Service. [Kunming android training place] @ Override protected void onStart () {// rewrite the onStart method dataReceiver = new DataReceiver (); IntentFilter filter = new IntentFilter (); // create an IntentFilter object filter. addAction ("com. justel. serviceBC "); registerReceiver (dataReceiver, filter); // register Broadcast guest er super. onStart () ;}stop service command main program activity can publish broadcast, used to pass data or control information to the background service, such as stop service command. BtnStop. setOnClickListener (new OnClickListener () {// Add Click Event listener @ Override public void onClick (View v) {// Override onClick method Intent myIntent = new Intent (); // create the Intent object myIntent. setAction ("com. justel. service "); myIntent. putExtra ("cmd", CMD_STOP_SERVICE); sendBroadcast (myIntent); // send broadcast }}); the receiving broadcast background service registers BroadCastReceiver to receive the broadcast @ Override public int onStartCommand (Intent intent, int flags, int startId) sent by the main program) {// override onStartCommand method IntentFilter filter = new IntentFilter (); // create an IntentFilter object filter. addAction ("com. justel. service "); registerReceiver (cmdReceiver, filter); // register Broadcast javaser doJob (); // call the method to start the thread and complete return super. onStartCommand (intent, flags, startId);} real-time sending backend servi ...... remaining full text>

How to dynamically generate multiple interfaces in android Development

I think we can.
1. parse the questions in excel to generate a set of question libraries. Of course, you must reserve the attributes you want to record for each question, including the results and types...
2. layout classification. For a single choice, you can write a single choice layout, multiple choice, multiple choice layout, and Q & A write a Q & A layout. These three la s are displayed based on the type of each question.
3. The previous question and next question are associated with the question library set, representing the location of the question library set, so that you can determine which type of layout the next question should display based on the question type.
In this way, the display mode can be controlled using only one interface and multiple sub-la S.

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.