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
- Layoutinflater Inflater = Layoutinflater.from (this);
- View layout = Inflater.inflate (R.layout.main, null);
The second method:
[Java]View Plaincopy
- Layoutinflater inflater = Getlayoutinflater ();
- View layout = Inflater.inflate (R.layout.main, null);
The third method:
[Java]View Plaincopy
- Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service);
- 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
- Public class Layoutinflateractivity extends Activity {
- private EditText et;
- private Button btn;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- //The first method
- Layoutinflater Inflater = Layoutinflater.from (this);
- View layout = Inflater.inflate (R.layout.main, null);
- //second method
- //Layoutinflater Inflater = Getlayoutinflater ();
- //View layout = inflater.inflate (R.layout.main, NULL);
- //Third method
- //Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service);
- //View layout = inflater.inflate (R.layout.main, NULL);
- //Here is a pre-obtained layout file to instantiate the specific control, and you can customize the control as appropriate
- ET = (EditText) Layout.findviewbyid (R.id.edittext);
- Et.setbackgroundcolor (Color.yellow);
- BTN = (Button) Layout.findviewbyid (R.ID.BTN);
- Btn.setbackgroundcolor (Color.cyan);
- //Display
- Setcontentview (layout);
- }
- }
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