What does LayoutInflater class do? (in Android) [c

2020-05-14 08:02发布

I am unable to understand the use of LayoutInflater in Android.

What exactly is the role of LayoutInflater, and how to use it for a simple Android app?

5条回答
甜甜的少女心
2楼-- · 2020-05-14 08:12

From the Android docs:

Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:

LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);

Useful in creating Custom Views

See

查看更多
神经病院院长
3楼-- · 2020-05-14 08:18

Some basics for LayoutInflater-

  • LayoutInflater is used to manipulate Android screen using predefined XML layouts.
  • This class is used to instantiate layout XML file into its corresponding View objects.

  • It is never used directly. Instead,

  • use getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context.
查看更多
放我归山
4楼-- · 2020-05-14 08:31

It is most likely to get closed but the role of layout inflater is to create a view from the supplied layout file

View row=getLayoutInflater.inflate(R.layout.list_item,parent,false);

it is creating the view from the R.layout.list_item file and proving that view.

查看更多
家丑人穷心不美
5楼-- · 2020-05-14 08:33

when you run setContentView(layout file name), you can run findViewById(id of the widget). You dont need to do something like xyz.findViewById. The context of your app is set to that layout file and all findBiewById call will refer to that layout file.

There are cases when you need to pick up one more layout file, like a CustomDialog, ListElement or a Custom Toast. At this time you wont want to create a Activity just for these small UI components, that that time you programmatically need to get a programmatic reference to your layout file, so that you can run findViewById on it.

Inflate, blows the layout like a balloon and give you the balloon for you to watch around it all the colors and objects drawn on it :). Inflate gives you the object reference to that layout to call findViewById on.

Hope this clears.

查看更多
姐就是有狂的资本
6楼-- · 2020-05-14 08:36

What is Layoutinflater ?

LayoutInflater is a class (wrapper of some implementation or service), you can get one:

LayoutInflater li = LayoutInflater.from(context);

How to use Layoutinflater ?

You feed it an XML layout file. You need not give full file address, just its resource id, generated for you automatically in R class. For example, a layout file which look like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <TextView
            android:id="@+id/text_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

</LinearLayout>

saved as /res/layout/my_layout.xml.

You give it to LayoutInflater like:

  View v = li.inflate(R.layout.my_layout,null,false);

What did Layout Inflater do ?

That v is now a LinearLayout object (LinearLayout extends View) , and contains a TextView object, arranged in exact order and with all properties set, as we described in the XML above.


TL;DR: A LayoutInflater reads an XML in which we describe how we want a UI layout to be. It then creates actual Viewobjects for UI from that XML.

查看更多
登录 后发表回答