【android】view.getRootView()的真正含义及测试

来源:互联网 发布:name.com 域名不能转出 编辑:程序博客网 时间:2024/04/29 14:00

view.getRootView()的官方解释就是:Finds the topmost view in the current view hierarchy.寻找当前的view层次中处在最顶层的view

我的理解就是找出该view实例所在的view层次的根view。


为证实这个view.getRootView()的真正含义,下面我做了测试:

activity_main.xml:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent">  
  4.     <include   
  5.       layout="@layout/test_layout"/>  
  6. </AbsoluteLayout>  

test_layout.xml:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <RelativeLayout   
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content">  
  9.         <Button      
  10.             android:id="@+id/testBtn"  
  11.             android:layout_width="match_parent"  
  12.             android:layout_height="wrap_content"/>  
  13.     </RelativeLayout>  
  14. </LinearLayout>  

MainActivity.java:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.   
  8.         Button testBtn = (Button) findViewById(R.id.testBtn);  
  9.         Log.i("testBtn", testBtn.toString()+"  id:"+testBtn.getId());  
  10.         Log.i("testBtn's RootView",testBtn.getRootView().toString()+"  id:"+testBtn.getRootView().getId());  
  11.           
  12.         View testView =LayoutInflater.from(this).inflate(R.layout.test_layout, null);  
  13.         Button testBtn2 = (Button) testView.findViewById(R.id.testBtn);  
  14.         Log.i("testBtn2", testBtn2.toString()+"  id:"+testBtn2.getId());  
  15.         Log.i("testBtn2's RootView",testBtn2.getRootView().toString()+"  id:"+testBtn2.getRootView().getId());  
  16.           
  17.         View decorView = getWindow().getDecorView();  
  18.         View contentView =decorView.findViewById(android.R.id.content);  
  19.         View mainRootView =((ViewGroup) contentView).getChildAt(0);  
  20.         Log.i("decorView", decorView.toString()+"  id:"+decorView.getId());  
  21.         Log.i("contentView", contentView.toString()+"  id:"+contentView.getId());  
  22.         Log.i("mainRootView",mainRootView.toString()+"  id:"+mainRootView.getId());  
  23.     }  
  24. }  

打印结果:



从打印结果我们需要注意的是testBtn、testBtn2虽然id相同,但却是不同的实例,它们所在的view层次也不一样,因此它们通过getRootView得到的根view是不一样的。


最后我们可以看出来,要想获得当前界面所用的xml文件的根view,就可以用

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. View rootView = ((ViewGroup) (getWindow().getDecorView().findViewById(android.R.id.content))).getChildAt(0);  

来获取。
0 0
原创粉丝点击