What is the use of LayoutInflater
in Android?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
LayoutInflater is a fundamental component in Android. You must use it all the time to turn xml files into view hierarchies.
LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
In other words, it takes as input an XML file and builds the View objects from it.
Here is another example similar to the previous one, but extended to further demonstrate inflate parameters and dynamic behavior it can provide.
Suppose your ListView row layout can have variable number of TextViews. So first you inflate the base item View (just like the previous example), and then loop dynamically adding TextViews at run-time. Using android:layout_weight additionally aligns everything perfectly.
Here are the Layouts resources:
list_layout.xml
schedule_layout.xml
Override getView method in extension of BaseAdapter class
Note different inflate method calls:
What does
LayoutInflator
do?When I first started Android programming, I was really confused by
LayoutInflater
andfindViewById
. Sometimes we used one and sometimes the other.LayoutInflater
is used to create a newView
(orLayout
) object from one of your xml layouts.findViewById
just gives you a reference to a view than has already been created. You might think that you haven't created any views yet, but whenever you callsetContentView
inonCreate
, the activity's layout along with its subviews gets inflated (created) behind the scenes.So if the view already exists, then use
findViewById
. If not, then create it with aLayoutInflater
.Example
Here is a mini project I made that shows both
LayoutInflater
andfindViewById
in action. With no special code, the layout looks like this.The blue square is a custom layout inserted into the main layout with
include
(see here for more). It was inflated automatically because it is part of the content view. As you can see, there is nothing special about the code.Now let's inflate (create) another copy of our custom layout and add it in.
To inflate the new view layout, all I did was tell the inflater the name of my xml file (
my_layout
), the parent layout that I want to add it to (mainLayout
), and that I don't actually want to add it yet (false
). (I could also set the parent tonull
, but then the layout parameters of my custom layout's root view would be ignored.)Here it is again in context.
Notice how
findViewById
is used only after a layout has already been inflated.Supplemental Code
Here is the xml for the example above.
activity_main.xml
my_layout.xml
When do you need LayoutInflater
RecyclerView
. (See theseRecyclerView
examples for a list or a grid.) You have to inflate a new layout for every single visible item in the list or grid.Layout inflater is a class that reads the xml appearance description and convert them into java based View objects.
Inflating means reading the XML file that describes a layout (or GUI element) and to create the actual objects that correspond to it, and thus make the object visible within an Android app.
This file could saved as date_time_dialog.xml:
This file could saved as date_time_picker.xml:
The
MainActivity
class saved as MainActivity.java: