美文网首页
android LayoutInflater用法

android LayoutInflater用法

作者: yezi1989 | 来源:发表于2018-07-01 11:35 被阅读3次

在实际开发中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);

相关文章

网友评论

      本文标题:android LayoutInflater用法

      本文链接:https://www.haomeiwen.com/subject/prucuftx.html