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等。 总之想怎么用就怎么用了...

  • 相关阅读:
    今天看了几个小时的微信小程序说说心得体会
    关于wordpress中的contact form7和WP Mail SMTP的一些设置
    关于163发邮件报错535 Error:authentication failed解决方法
    Numpy 基本除法运算和模运算
    基本的图像操作和处理
    Python中flatten用法
    media
    TensorFlow模型保存和提取方法
    docker 默认用户和密码
    Windows安装TensorFlow
  • 原文地址:https://www.cnblogs.com/homg/p/3345011.html
Copyright © 2011-2022 走看看