inflater用法

来源:互联网 发布:淘宝宝贝下架时间 编辑:程序博客网 时间:2024/06/05 06:11

LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

实现LayoutInflater的实例化共有3种方法,

(1).通过SystemService获得

    LayoutInflaterinflater = (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);

    Viewview = inflater.inflate(R.layout.main, null);

(2).从给定的context中获得

    LayoutInflaterinflater = LayoutInflater.from(context);

    Viewview = inflater.inflate(R.layout.mian, null);

(3).

    LayoutInflaterinflater =getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)

    Viewlayout = inflater.inflate(R.layout.main, null);

其实,这三种方式本质是相同的,从源码中可以看出:

getLayoutInflater():

Activity的getLayoutInflater()方法是调用PhoneWindow的getLayoutInflater()方法,看一下该源代码:

    publicPhoneWindow(Contextcontext) {

        super(context);

        mLayoutInflater= LayoutInflater.from(context);

    }

可以看出它其实是调用LayoutInflater.from(context)。

LayoutInflater.from(context):

    public static LayoutInflaterfrom(Context context) {

        LayoutInflaterLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(LayoutInflater== null){

            thrownew AssertionError("LayoutInflaternot found.");

        }

        returnLayoutInflater;

    }

可以看出它其实调用context.getSystemService()。

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

其余几个重载的inflate函数类似。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 自动挡汽车没电打不着火怎么办 自动挡汽车有电打不着火怎么办 p0846骐达故障怎么办 汽车冷却液漏了怎么办 冷却液管子漏了怎么办 电脑网络不可用怎么办 手表带子坏了怎么办 这几天生意不好怎么办 大学不想住宿舍怎么办 在北京买车后被骗怎么办 联通套餐不到期怎么办 联通全国流量包怎么办 缤智车钥匙丢了怎么办 车遮阳板松了怎么办 洗车把内饰划了怎么办 新车销售没销量怎么办 住院时间重叠了怎么办 长安cs75油耗高怎么办 墙内线烧了怎么办 如果没买票想进高铁站怎么办 老赖拒绝还款怎么办 gta5资产不兼容怎么办 平板黑屏闪退怎么办 施工证学历不够怎么办 隧道放炮声大怎么办 58工作被骗了怎么办 学历国家不承认怎么办 福州居住证一年到期怎么办 居住证过期2年怎么办 被房东坑了怎么办 房东违反了合同怎么办 上海居住证过期了怎么办 地铁卡消磁了怎么办 学生卡消磁了怎么办 家属院没房产证怎么办 邮箱收不到邮件怎么办 大学宿舍八人间怎么办 高中档案袋拆了怎么办 公派教师回国后怎么办? 事业单位辞职后档案怎么办 学信支付不了怎么办