findViewById()与Inflate()和setContentView()关系扯谈

关于findViewById()与Inflate()和setContentView()的关系我理了理,清晰多了,觉得很有必要记一记。

1.findViewById()与Inflate()的关系
findViewById()其实好理解,从layout上把控件找出来并转为一个对应的对象。
至于Inflate()则是把layout从xml文件中实例化成一个对象。这样findViewById才能在上面找出控件。

2. Inflate()与setContentView()的关系
如果仅从上面这样看Inflate()的功能,setContentView()效果也是一样的。实际也上也是把xml文件上的layout实例化显示.
如: setContentView(R.layout.main);
不过setContentView()一运行,界面马上就显示。Inflate()则只生成和操作对象,但是隐藏状态,不显示在界面上。显示还是要靠setContentView()。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // LayoutInflater inflater=getLayoutInflater();  
  2. public class MyInflate extends Activity{    
  3.     public void OnCreate(Bundle savedInstanceState){  
  4.         super.onCreate(savedInstanceState);   
  5.   
  6.         //默认方法使用setContentView  
  7.         //setContentView(R.layout.main);  
  8.   
  9.         //inflate()把布局实例化后,再显示在界面上  
  10.         LayoutInflater inflate = LayoutInflater.from(this);  
  11.         View vw = inflate.inflate(R.layout.main,null);  
  12.         setContentView(vw);  
  13.     }  
  14. }  
3. 那Inflate()有什么价值呢?
       Inflate()主要用在当前Activity中,控制别的layout,Fragment就经常用。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public View onCreateView(LayoutInflater inflater,  
  2.                      ViewGroup container,  
  3.                      Bundle savedInstanceState)    
  4.    
  5.     inflater.inflate(R.layout.myfragment,container,false);  
弹出窗口这类也是,通过Inflate()实现化出弹出窗口,然后再用findViewById()控制上面控件.
   其实这个和CS界面开发时的搞法差不多。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">  </span>  View dgVw=View.inflate(this,R.layout.dialog_layout,null);  
  2.       TextView dgtv=(TextView)dgVw.findViewById(R.id.dgtv);  
  3.       dgtv.setText("说好的年终奖呢?");  
4.附注:    
 另外附上从http://www.2cto.com/kf/201205/131136.html找来的说明,
 这篇文章还进源码查看了一翻。发现其实本质都是调用context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES)方法得到的。

 实现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);

 Android关于这部份的源码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. LayoutInflater.from(context):  
  2.     public static LayoutInflaterfrom(Context context) {  
  3.         LayoutInflaterLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  4.         if(LayoutInflater== null){  
  5.             thrownew AssertionError("LayoutInflaternot found.");  
  6.         }  
  7.         returnLayoutInflater;  
  8.     }  



MAIL:  xcl_168@aliyun.com

BLOG: http://blog.csdn.net/xcl168

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值