Inflator in Android Application Development

前端 未结 4 1900
不知归路
不知归路 2021-02-14 14:30

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:46

    layout inflator is used to return a java object of your complete layout

    suppose you have a layout xml file in which the root element is relative layout and it contains a imageview and textview then using layout inflator you can return a view object that refers to entire layout.

    this basically is used in list view and grid view to plug into them a layout object of single row or element which is to be repeated.

    0 讨论(0)
  • 2021-02-14 14:58

    Not quite sure what you mean, but if its related with inflating views, its used to load layout xml files into your application. e.g by

    View myWelcome = View.inflate(this, R.layout.welcome, null);
    

    Its easier and consider best practice to have you view definition inside layout xml files, instead of creating your views fully by code.

    0 讨论(0)
  • 2021-02-14 15:04

    you were asking for use of Inflator.. basically when you want to use two xml files in one java class ,inflator is used and its code is simple which is given below..

     TextView text;
        View layout;
            LayoutInflater inflator=getLayoutInflater();
            layout =inflator.inflate(R.layout.new_xml_that you want to use in that java class,null);
    
            text=(TextView)layout.findViewById(R.id.text);
            text.setText("progressing");
    

    here i use textview,this is present in next xml with id=text thats it.. if you find this worthy then please like this.. thanks

    0 讨论(0)
  • 2021-02-14 15:05

    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.

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