What does it mean to inflate a view from an xml file?

前端 未结 6 1737
情书的邮戳
情书的邮戳 2020-11-28 17:35

I am new to android development and keep coming across references to Inflating views from a layout xml file. I googled and searched the development guide but still wasn\'t

相关标签:
6条回答
  • 2020-11-28 17:41

    Inflating is the process of adding a view (.xml) to activity on runtime. When we create a listView we inflate each of its items dynamically. If we want to create a ViewGroup with multiple views like buttons and textview, we can create it like so:

    Button but = new Button();
    but.setText ="button text";
    but.background ...
    but.leftDrawable.. and so on...
    
    TextView txt = new TextView();
    txt.setText ="button text";
    txt.background ... and so on...
    

    Then we have to create a layout where we can add above views:

    RelativeLayout rel = new RelativeLayout();
    
    rel.addView(but);
    

    And now if we want to add a button in the right-corner and a textview on the bottom, we have to do a lot of work. First by instantiating the view properties and then applying multiple constraints. This is time consuming.

    Android makes it easy for us to create a simple .xml and design its style and attributes in xml and then simply inflate it wherever we need it without the pain of setting constraints programatically.

    LayoutInflater inflater = 
                  (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
    //now add menuLayout to wherever you want to add like
    
    (RelativeLayout)findViewById(R.id.relative).addView(menuLayout);
    
    0 讨论(0)
  • 2020-11-28 17:50

    A layman definition for inflation might be to convert the XML code to Java code. Just a way to understand, e.g., if we have a tag in XML, OS has to create a corresponding Java object in memory, so inflatter reads the XMLtags, and creates the corresponding objects in Java.

    0 讨论(0)
  • 2020-11-28 17:51

    "Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup. When you call setContentView(), it attaches the views it creates from reading the XML to the activity. You can also use LayoutInflater to add views to another ViewGroup, which can be a useful tool in a lot of circumstances.

    0 讨论(0)
  • 2020-11-28 17:58

    Because we make UI into XML but view objects is what we display so we somehow need to convert xml into view objects so inflating means we are converting xml into view objects so that it can be displayed, for this we need a service called layout inflator service and give it an xml and it will be convert for you.

    0 讨论(0)
  • 2020-11-28 18:00

    When you write an XML layout, it will be inflated by the Android OS which basically means that it will be rendered by creating view object in memory. Let's call that implicit inflation (the OS will inflate the view for you). For instance:

    class Name extends Activity{
        public void onCreate(){
             // the OS will inflate the your_layout.xml
             // file and use it for this activity
             setContentView(R.layout.your_layout);
        }
    }
    

    You can also inflate views explicitly by using the LayoutInflater. In that case you have to:

    1. Get an instance of the LayoutInflater
    2. Specify the XML to inflate
    3. Use the returned View
    4. Set the content view with returned view (above)

    For instance:

    LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
    View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
    setContentView(theInflatedView) // 4
    
    0 讨论(0)
  • 2020-11-28 18:05

    I think here "inflating a view" means fetching the layout.xml file drawing a view specified in that xml file and POPULATING ( = inflating ) the parent viewGroup with the created View.

    0 讨论(0)
提交回复
热议问题