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

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

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

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

activity_main.xml:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  	<include
      layout="@layout/test_layout"/>
</AbsoluteLayout>

test_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/testBtn"
	   		android:layout_width="match_parent"
	   		android:layout_height="wrap_content"/>
    </RelativeLayout>
</LinearLayout>

MainActivity.java:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button testBtn = (Button) findViewById(R.id.testBtn);
		Log.i("testBtn", testBtn.toString()+"  id:"+testBtn.getId());
		Log.i("testBtn's RootView",testBtn.getRootView().toString()+"  id:"+testBtn.getRootView().getId());

		View testView =LayoutInflater.from(this).inflate(R.layout.test_layout, null);
		Button testBtn2 = (Button) testView.findViewById(R.id.testBtn);
		Log.i("testBtn2", testBtn2.toString()+"  id:"+testBtn2.getId());
		Log.i("testBtn2's RootView",testBtn2.getRootView().toString()+"  id:"+testBtn2.getRootView().getId());

		View decorView = getWindow().getDecorView();
		View contentView =decorView.findViewById(android.R.id.content);
		View mainRootView =((ViewGroup) contentView).getChildAt(0);
		Log.i("decorView", decorView.toString()+"  id:"+decorView.getId());
		Log.i("contentView", contentView.toString()+"  id:"+contentView.getId());
		Log.i("mainRootView",mainRootView.toString()+"  id:"+mainRootView.getId());
	}
}

打印结果:

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

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

View rootView = ((ViewGroup) (getWindow().getDecorView().findViewById(android.R.id.content))).getChildAt(0);

来获取。

时间: 2025-01-21 10:51:23

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

Android View 事件分发机制 源码解析 (上)

一直想写事件分发机制的文章,不管咋样,也得自己研究下事件分发的源码,写出心得~ 首先我们先写个简单的例子来测试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个MyButton继承Button,然后把跟事件传播有关的方法进行复写,然后添加上日志~ MyButton [java] view plain copy package com.example.zhy_event03; import android.content.Context; import andr

Android View measure (三) 常用方法

ViewGroup.measureChildren() ViewGroup.measureChild() ViewGroup.measureChildWithMargins() /** * Ask one of the children of this view to measure itself, taking into * account both the MeasureSpec requirements for this view and its padding * and margins

Android View框架总结(二)View焦点

请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52263256 前言:View框架写到第六篇,发现前面第二篇竟然没有,然后事情是在微信公众号发了,忘记在博客上更新,所以关注微信公众号的应该都看过了,趁今天有时间遂补上.(PS:本篇文章中源码均是android 6.0,请知晓) 本来之前说view下篇是写onMeasure,onLayou,onDraw相关的,笔者做盒子开发,遥控器按键,碰到的都是焦点控制相关.所以先

[Android][转]Android View绘制13问13答

转自:http://www.androidchina.net/4458.html 1.view的绘制流程分几步,从哪开始?哪个过程结束以后能看到view? 答:从ViewRoot的performTraversals开始,经过measure,layout,draw 三个流程.draw流程结束以后就可以在屏幕上看到view了. 2.view的测量宽高和实际宽高有区别吗? 答:基本上百分之99的情况下都是可以认为没有区别的.有两种情况,有区别.第一种 就是有的时候会因为某些原因 view会多次测量,那

Android View系统分析之二View与ViewGroup

目录 在Android View系统分析之从setContentView说开来(一)一文中,我们从setContentView开始阐述了Android中的视图层次,从设置内容布局到整个视图层次的建立的过程.并且对View和ViewGroup的关系进行了简单的介绍,今天我们继续来深入的了解Android中的View和ViewGroup. ViewGroup与View的关系 我们在定义一个布局时,在它的顶层通常都是使用LinearLayout或者RelativeLayout等组件来包装一些子控件,例

Android jPBC 2.0.0配置与测试

我在前面的一片博客中,介绍了jPBC 2.0.0在PC平台上面的配置和测试.既然jPBC是Java平台上面实现的,那么jPBC能不能在Android这个以Java为主要语言的平台上运行呢?这样一来,各种在jPBC上撰写的有关双线性对的函数就都能够在移动终端上面用了.我个人的想法就是把最新的密码学算法应用到工程里面,而这确实是我想法的一个很好的跨越.因此,我在第一时间公开整个配置的过程以及我测试的方法,以供广大国内密码学研究者们进行尝试.整个配置过程实际上是非常简单的,这也要感谢jPBC库的编写者

android View 详解

android.View.View(即View)类是以矩形的方式显示在屏幕上,View是用户界面控件的基础.View的继承层次关系如下图: 可以看到所有的界面控件都是View的子类.简单证实一下,每当你用findViewByIds(R.id.xx)时总要将其强转,因为该方法返回的是一个View实例,有木有!!!其中不得不提View的subClass ViewGroup.Android系统中的所有UI类都是建立在View和ViewGroup这两个类的基础上的.所有View的子类成为"Widget&

Android View体系(十一)自定义ViewGroup

相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源码解析Scroller Android View体系(五)从源码解析View的事件分发机制 Android View体系(六)从源码解析Activity的构成 Android View体系(七)从源码解析View的measure流程 Android View体系(八)从源码解析View的layout

在Android studio中进行单元测试和ui测试

1.配置支持单元测试的工程 在写测试之前,让我们做下简单的检查,确保工程配置正确. 首先,确认在Build Variants窗口内的Test Artifact中选择了"Unit Tests". 然后,在工程的src文件夹内创建test和test/java文件夹.需要注意的是,你不能在Android视图下进行这些操作,要么在系统的文件管理器内创建,要么在工程窗口左上方点击下拉菜单选择Project视图.最终的工程结构应该是这样的: (在codelab的剩余部分,你可以返回继续使用Andr