zoukankan      html  css  js  c++  java
  • android中LayoutInflater详解与使用

    android的LayoutInflater用来得到一个布局文件,也就是xxx.xml,而我们常用的findviewbyid是用来取得布局文件里的控件或都布局。inflater即为填充的意思,也就是说得到一个布局文件来填充(把布局文件实例化成一个View然后返回该view)。使得方法如下: 三种办法可以得到:

    1:在activity中的话可以直接调用getLayoutInflater()来获得。

    2:通过服务获得:getSystemService. (Context.LAYOUT_INFLATER_SERVICE)。

    3:通过ayoutInflater.from(Context context)方法来获得。 三种方法的代码如下:

    1 //方法3:
    2  LayoutInflater lf=LayoutInflater.from(context); 
    3  View tempView=lf.inflate(R.layout.select_list, null);
    4   //方法2:
    5   LayoutInflater lf = (LayoutInflater)context.getSystemService.(Context.LAYOUT_INFLATER_SERVICE); 
    6  View tempView=lf.inflate(R.layout.select_list, null); 
    7  //方法1: 
    8 LayoutInflater lf = LayoutInflater.from(context);
    9  View tempView=lf.inflate(R.layout.select_list, null);

    在activity中的话上面的context可直接换成this。 得到返回的View后我们可以用他来给布局文件里的控件进行相应设置,如:

    1 TextView id=(TextView) tempView.findViewById(R.id.id); 
    2 TextView address=(TextView) tempView.findViewById(R.id.address); id.setText("123456"); 
    3 address.setText("中国湖南长沙"); 

    上面的获得View方法有两个参数,lf.inflate(int resource, ViewGroup root),第一个为整型的资源ID,也就是xml布局文件的id,后面为根View,如果为null则只创建和返回View,如果传入root,则把创建的View加为根View的子view。 LayoutInflater可以用来得到布局文件对里面的控件进行设置,也可以可以将得到的View返回给fragment,adapter等。 总之想怎么用就怎么用了...

  • 相关阅读:
    fish shell version
    golang io.ReadFull
    Unity5 2D Animation
    unity3d vscode
    golang bufio.Scanner
    kafka知识点
    linux clone
    Oracle查询在哪些 存储过程/函数/触发器 等等中包含 指定字符串
    在Oracle中,使用简洁的函数(Function)实现字符串split分割效果
    在Spring中,使用ProxyFactory实现对Cglib代理对象的再次代理
  • 原文地址:https://www.cnblogs.com/homg/p/3345011.html
Copyright © 2011-2022 走看看