Android一分鐘
-
作用:把xml類型的布局轉(zhuǎn)化成相應(yīng)的View對象
- LayoutInflater inflater = getLayoutInflater(); //調(diào)用Activity的getLayoutInflater()
- LayoutInflater inflater = LayoutInflater.from(context);
- LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
inflater.inflater
inflate(int resource, ViewGroup root, boolean attachToRoot)
inflate(int resource, ViewGroup root)//attachToRoot = true
-
If attachToRoot is set to true, then the layout file specified in the first parameter is inflated and attached to the ViewGroup specified in the second parameter.
- 如果attachToRoot=true刊苍,view對象將直接綁定到ViewGroup
-
When attachToRoot is false, the layout file from the first parameter is inflated and returned as a View.
- 如果attachToRoot=false解寝,view對象將被填充并且返回該對象,但是不會綁定到ViewGroup上摘投,需要手動addView()綁定
-
attachToRoot 必須設(shè)置為 false的情況,例如:
-
recyclerview
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(android.R.layout.list_item_recyclerView, parent, false); return new ViewHolder(view); }
由于recyclerview會負責填充和綁帶viewholder
-
Fragment
-
由于FragmentManager負責add, remove and replace Fragments
public View onCreateView(LayoutInflater inflater, ViewGroup parentViewGroup, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout, parentViewGroup, false); return view; }
-
-