View.findViewById() vs Activity.findViewById()

来源:互联网 发布:酒店网络点评回复 编辑:程序博客网 时间:2024/04/29 19:40

网上看见View.findViewById() 和 Activity.findViewById()执行效率不一样

通常我们使用Activity.findViewById()如:

Java代码  收藏代码
  1. TextView tv_inner_1 = (TextView)this.findViewById(R.id.tv_inner_1);   
  2. TextView tv_inner_2 = (TextView)this.findViewById(R.id.tv_inner_2);  

 View.findViewById() 如:

Java代码  收藏代码
  1. View layout_outer = this.findViewById(R.id.layout_outer);   
  2. TextView tv_inner_1 = (TextView)layout_outer.findViewById(R.id.tv_inner_1);   
  3. TextView tv_inner_2 = (TextView)layout_outer.findViewById(R.id.tv_inner_2);  

 

他们都是针对下面同一个xml

Java代码  收藏代码
  1. <LinearLayout>   
  2.      <LinearLayout id="@+id/layout_outer">   
  3.            <TextView id="@+id/tv_inner_1"/>   
  4.            <TextView id="@+id/tv_inner_2"/>   
  5.      </LinearLayout>   
  6. </LinearLayout>  
0 0