Inflator in Android Application Development

前端 未结 4 1610
自闭症患者
自闭症患者 2021-02-14 14:10

Can anybody please tell What Inflator is and how it is being used in an Android application?

I don\'t know the exact use of it and Why it is being used.

4条回答
  •  情话喂你
    2021-02-14 14:55

    My preferred way to handle inflation:

    //First get our inflater ready, you'll need the application/activity context for this
    LayoutInflater mInflater;
    mInflater = LayoutInflater.from(mContext);
    
    //Inflate the view from xml
    View newView = mInflater.inflate(R.layout.my_new_layout, null);
    
    //Then you'll want to add it to an existing layout object
    mMainLayout.add(newView);
    
    //Or perhaps just set it as the main view (though this method can also 
    // inflate the XML for you if you give it the resource id directly)
    setContentView(newView);
    

    Basically, you use it to inflate existing xml layouts at runtime. Usually you go ahead and insert those new views into previously defined ViewGroups or List objects.

提交回复
热议问题