Android uses XML-related skills

Source: Internet
Author: User

You may still remember that we introduced the Android ListView application in detail in the previous article, which is mainly for visual programming. Here, we will use XML for Android to introduce you to another visual programming method.

Just like the cross-platform UI library, Android uses XML files to store the layout of interface elements. Currently, some popular interface components use XML for Android.

In Android, the res/layout Resource Directory contains one or more. xml files. This is the layout file of the interface. Let's open one. I open the res/layout/main. xml file in the current project directory.

 
 
  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < LinearLayout xmlns:android=
    "http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. < TextView   
  7. android:layout_width="fill_parent"   
  8. android:layout_height="wrap_content"   
  9. android:text="@string/hello" 
  10. android:id="@+id/mainview" 
  11. /> 
  12. < /LinearLayout> 

The simple layout of this file refers to the use of a LinearLayout layout, which contains only one TextView interface element, that is, a View. When the Activity loads the View, it can be directly loaded in onCreate. This. setContentView (R. layout. main); where R. layout. main is a prime value, which is compiled and generated by the android development environment and mapped to res/layout/main. xml.

Therefore, setContentView (R. layout. main); is equivalent to configuring a layout according to the layout of main. xml and then loading it, which is consistent with the following code.

 
 
  1. LinearLayout layout = new LinearLayout(this);  
  2. TextView tv = new TextView(this);  
  3. tv.setText(R.string.hello);  
  4. layout.addView(tv);  
  5. this.setContentView(layout); 

Here, R. string. hello is also a resource ing ID, which is the value corresponding to hello in res/values/string. xml.

This section describes how to use XML for Android.

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.