转 view.getRootView()的真正含义及测试

http://www.tuicool.com/articles/bi6Jze

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

来获取。



本人的一些笔记:

如果

DecorView.layout(0, 0, width, height);

view.draw(canvas);

截出来的图,上面会有白色一层DecorView高度包含通知栏高度

2.

activity.findViewById(android.R.id.content).layout(0, 0, width, height);

view.draw(canvas);

截出来的图,顶部无白色,但整个activity上移,因为content+顶部标题栏高度(空白区域)=DecorView

3.r.id.content是一个FrameLayout

他的child(0)=你定义的布局

 view = ((FrameLayout)activity.findViewById(android.R.id.content)).getChildAt(0);

view.draw(canvas);

这样截图就没有任何问题了!!




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值