<!--[if !supportLists]-->1 <!--[endif]-->android<!--[if !supportNestedAnchors]--><!--[endif]-->.view.LayoutInflater
LayoutInflater用于动态加载布局,实现XML布局文件转化为View视图。LayoutInflater中inflate方法就是用于将XML布局文件转化为View视图。有四种方式:
public android.view.View inflate(int resource, android.view.ViewGroup root) |
public android.view.View inflate(org.xmlpull.v1.XmlPullParser parser, android.view.ViewGroup root) |
public android.view.View inflate(int resource, android.view.ViewGroup root, boolean attachToRoot) |
public android.view.View inflate(org.xmlpull.v1.XmlPullParser parser, android.view.ViewGroup root, boolean attachToRoot) |
<!--[if !supportLists]-->1.1 <!--[endif]-->LayoutInflater获取方式
如果继承自android.app.Fragment,Fragment有个onCreateView方法,其第一个参数就是LayoutInflater。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) |
通过这种方式就可以获得LayoutInflater。
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) |
LayoutInflater inflater = LayoutInflater.from(Activity.this) |
LayoutInflater inflater = getLayoutInflater(); |
例子
public View getView(int position, View view, ViewGroup parent) { ImageView image = null; TextView title = null; TextView text = null; if (view == null || position < listData.size()) { view = LayoutInflater.from(mContext).inflate(R.layout.contacts_item, null); image = (ImageView) view.findViewById(R.id.ItemImage); title = (TextView) view.findViewById(R.id.ItemTitle); text = (TextView) view.findViewById(R.id.ItemText); } //绘制联系人名称 title.setText((String) listData.get(position).get("ItemTitle")); //绘制联系人号码 text.setText((String) listData.get(position).get("ItemText")); //绘制联系人头像 image.setImageBitmap((Bitmap) listData.get(position).get("ItemImage")); return view; } |