LayoutInflater 类的使用

一个Activity里如果直接用findViewById(),对应的是setConentView()的那个layout里的组件,因此如果你的

 Activity里如果用到别的layout,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对

象去找到它上面的组件,

 

 

 

public View inflate(Context context, int Resourece,ViewGroup root)
  作用:填充一个新的视图层次结构从指定的XML资源文件中
  context : The Context object for your activity or application
  reSource:View的layout的ID
  root: 生成的层次结构的根视图
  return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。
 

 

 

  View view = View.inflate(this, R.layout.dialog_layout, null);

  TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);

  dialogTV.setText("abcd");

 

  setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,

 

 

  有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,

  然后才能使用findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)

  但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。

  LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化。

 

 

 

LayoutInflater inflater = LayoutInflater.from(this);     
View view=inflater.inflate(R.layout.ID, null);    
//或者干脆并成一句:    
View view=LayoutInflater.from(this).inflate(R.layout.ID, null); 

 

或者:

 

 

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    
View view=inflater.inflate(R.layout.ID, null); 

 


android.view.View类中也有一个inflater方法:

 

 

public static View inflate (Context context, int resource, ViewGroup root)


Inflate a view from an XML resource. This convenience method wraps the LayoutInflater class, which provides a full range of options for view inflation.
Parameters

context	The Context object for your activity or application.
resource	The resource ID to inflate
root	A view group that will be the parent. Used to properly inflate the layout_* parameters.
 

该方法和LayoutInflater中的inflater方法区别不明,查api也没看明白:

 

 

public View inflate (int resource, ViewGroup root)
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters

resource	ID for an XML layout resource to load (e.g., R.layout.main_page)
root	Optional view to be the parent of the generated hierarchy.

Returns
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.
 

一般在adapter的getview方法中使用View.inflate的比较多,最近在研究自定义下拉刷新listview的时候,使用了View.inflate一直报如下错误:

 

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRefreshView = (RelativeLayout) mInflater.inflate(R.layout.pull_to_refresh_header, this, false);
        //mRefreshView = (RelativeLayout)View.inflate(context, R.layout.pull_to_refresh_header, null);
 

 

 

android.view.InflateException: Binary XML file line #10: Error inflating class com.markupartist.android.widget.PullToRefreshListView
 

而使用 LayoutInflater 就没事,原因翻阅很多资料都没弄明白,看样子定义控件的inflate还是使用

LayoutInflater 吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值