3 ways Android dynamically instantiates XML layouts

Source: Internet
Author: User

In the actual work, the pre-written layout files often do not meet our needs, sometimes according to the situation in the code to customize the control, which requires the use of layoutinflater.
Layoutinflater is meant to be "extended" in Android, acting like Findviewbyid (), unlike Layoutinflater, which is used to get the layout file object, and

Findviewbyid () is used to obtain a specific control. Layoutinflater is often used in the GetView method of Baseadapter to get the entire view and return.
There are three ways to use Layoutinflater:

The first method:

[Java]View Plaincopy
    1. Layoutinflater Inflater = Layoutinflater.from (this);
    2. View layout = Inflater.inflate (R.layout.main, null);

The second method:

[Java]View Plaincopy
    1. Layoutinflater inflater = Getlayoutinflater ();
    2. View layout = Inflater.inflate (R.layout.main, null);

The third method:

[Java]View Plaincopy
    1. Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service);
    2. View layout = Inflater.inflate (R.layout.main, null);

The essence of the first method is to call the third method, and the second method and the third method what is the difference, I really do not know, who knows please leave a message to teach Ah!

The following are simple examples of use:

[Java]View Plaincopy
  1. Public class Layoutinflateractivity extends Activity {
  2. private EditText et;
  3. private Button btn;
  4. @Override
  5. public void OnCreate (Bundle savedinstancestate) {
  6. super.oncreate (savedinstancestate);
  7. //The first method
  8. Layoutinflater Inflater = Layoutinflater.from (this);
  9. View layout = Inflater.inflate (R.layout.main, null);
  10. //second method
  11. //Layoutinflater Inflater = Getlayoutinflater ();
  12. //View layout = inflater.inflate (R.layout.main, NULL);
  13. //Third method
  14. //Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service);
  15. //View layout = inflater.inflate (R.layout.main, NULL);
  16. //Here is a pre-obtained layout file to instantiate the specific control, and you can customize the control as appropriate
  17. ET = (EditText) Layout.findviewbyid (R.id.edittext);
  18. Et.setbackgroundcolor (Color.yellow);
  19. BTN = (Button) Layout.findviewbyid (R.ID.BTN);
  20. Btn.setbackgroundcolor (Color.cyan);
  21. //Display
  22. Setcontentview (layout);
  23. }
  24. }

In addition, Getsystemservice is a method in activity that obtains the corresponding service object based on the name passed in, and these service name parameters are constants in the context class:

Description of the object returned by the incoming name
Window_service WindowManager managing open Windows programs
Layout_inflater_service Layoutinflater Gets the view defined in the XML
Activity_service Activitymanager managing the System state of an application
Power_service Powermanger power Supply Services
Alarm_service Alarmmanager Alarm Clock Service
Notification_service Services for Notificationmanager status bar
Keyguard_service Keyguardmanager Keypad Lock Service
Location_service Locationmanager Location Services, such as GPS
Search_service Searchmanager Search for services
Vebrator_service vebrator mobile phone Shake service
Connectivity_service CONNECTIVITY Network-connected services
Wifi_service Wifimanager Wi-Fi service
Telephony_service Teleponymanager Telephone Service

Reprint Address: http://disanji.net/2011/05/04/android-layoutinflater-usage/

3 ways Android dynamically instantiates XML layouts

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.