在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。
不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;
而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
列如:
rootView = LayoutInflater.from(context).inflate(R.layout.weather_forecast_slide_item, null);
tvWeek = (TextView) rootView.findViewById(R.id.tv_week);
tvDate = (TextView) rootView.findViewById(R.id.tv_date);
网友评论