Android之View.getRootView


view.getRootView的官方解释是
Finds the topmost view in the current view hierarchy
那这个顶层的View究竟是xml的最外层布局,还是getDecorView,抑或其他的呢?
下面通过测试来了解getRootView的真正含义,定义xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/linChangeSkinRootView"
android:layout_height="match_parent"
android:orientation="vertical">

//...

<Button
android:id="@+id/btnActCahngeSkinShowStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="40dp"
tools:text="演示台" />
</LinearLayout>
定义测试源码,
lg.e("getDecorView id:" + getWindow().getDecorView().getId());
lg.e("android.R.id.content id :" + getWindow().getDecorView().findViewById(android.R.id.content).getId());
lg.e("getDecorView-getChildAt 0 id :" + ((ViewGroup) (getWindow().getDecorView().findViewById(android.R.id.content))).getChildAt(0).getId());

lg.e("Act_change_skin的根布局id:" + generateView(R.id.linChangeSkinRootView).getId());
lg.e("btnActCahngeSkinShowStyle id :" + btnActCahngeSkinShowStyle.getId());

lg.e("Act_change_skin的getRootView id :" + generateView(R.id.linChangeSkinRootView).getRootView().getId());
lg.e("btnActCahngeSkinShowStyle的getRootView id :" + btnActCahngeSkinShowStyle.getRootView().getId());
打印结果:

从运行结果可以看到,xml的根布局应该对应((ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content))).getChildAt(0)。





免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM