LayoutInflater
程序员文章站
2024-02-10 23:48:46
...
Android一分钟
-
作用:把xml类型的布局转化成相应的View对象
- LayoutInflater inflater = getLayoutInflater(); //调用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 必须设置为 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; }
-
-
推荐阅读
-
LayoutInflater使用
-
LayoutInflater
-
LayoutInflater Factory
-
Android开发实现自定义Toast、LayoutInflater使用其他布局示例
-
Android LayoutInflater深入分析及应用
-
Android 中LayoutInflater.inflate()方法的介绍
-
Android LayoutInflater加载布局详解及实例代码
-
Android 中LayoutInflater.inflate()方法的介绍
-
Android LayoutInflater加载布局详解及实例代码
-
基于Android LayoutInflater的使用介绍