zoukankan      html  css  js  c++  java
  • 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)。




  • 相关阅读:
    SpringBoot-14-MyBatis预热篇,MySQL小结
    SpringBoot-13-插曲之Node文件重命名+自动生成json对象
    八月十九风雨大作
    诉世书
    《仪式》
    珊瑚墓地
    新生
    《应龙》
    《枝·你是树的狂舞》
    golang中使用etcd
  • 原文地址:https://www.cnblogs.com/linux007/p/5783115.html
Copyright © 2011-2022 走看看