美文网首页
Activity View 层级 1

Activity View 层级 1

作者: 比目鱼26 | 来源:发表于2017-04-13 14:04 被阅读0次

通过log的方式查看Activity View 层级

测试环境

sdk版本

    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.wsl.demo0"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

代码

public class MainActivity extends AppCompatActivity {

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

        View rootView = findViewById(R.id.root_demo);
        while (rootView != null) {
            Log.d("wsl", rootView.getClass().toString());
            rootView = (View) rootView.getParent();
        }
    }
}

xml如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_demo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.wsl.demo0.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这里的Constraint就是内容区域

输出

AppCompatActivity的结构如下:

04-13 13:28:52.350 16429-16429/? D/wsl: class android.support.constraint.ConstraintLayout
04-13 13:28:52.350 16429-16429/? D/wsl: class android.support.v7.widget.ContentFrameLayout
04-13 13:28:52.350 16429-16429/? D/wsl: class android.support.v7.widget.ActionBarOverlayLayout
04-13 13:28:52.350 16429-16429/? D/wsl: class android.widget.FrameLayout
04-13 13:28:52.350 16429-16429/? D/wsl: class android.widget.LinearLayout
04-13 13:28:52.350 16429-16429/? D/wsl: class com.android.internal.policy.impl.PhoneWindow$DecorView

如果把AppCompatActivity换成FragmentActivity输出如下:

D/wsl     (30101): class android.support.constraint.ConstraintLayout
D/wsl     (30101): class android.widget.FrameLayout
D/wsl     (30101): class android.widget.LinearLayout
D/wsl     (30101): class com.android.internal.policy.impl.PhoneWindow$DecorView

换成Activity的输出如下:

D/wsl     ( 3860): class android.support.constraint.ConstraintLayout
D/wsl     ( 3860): class android.widget.FrameLayout
D/wsl     ( 3860): class android.widget.LinearLayout
D/wsl     ( 3860): class com.android.internal.policy.impl.PhoneWindow$DecorView

结论

可以看出FragmentActivity和Activity的结构一样,AppCompatActivity是support library的一部分,查看源码可以发现是继承于FragmentActivity

public class AppCompatActivity extends FragmentActivity  {
......
}

DecorView在上一篇介绍过,首先是一个FrameLayout,其次是PhoneWindow的内部类,最重要的是位于整个View的顶层,�获取饮用的方式:

getWindow().getDecorView();

当用如下代码获取Content内容区域的父级时,需要注意一下

View view = findViewById(android.R.id.content);

1, 如果是FragmentActivity和Activity容器,view对应上图中的FrameLayout.
2, 如果是AppCompatActivity容器,view对应上图中的ContentFrameLayout.
上一篇介绍过ContentFrameLayout,
其实也是个自定义的FrameLayout, 如下:

/**
 * @hide
 */
public class ContentFrameLayout extends FrameLayout {

    public interface OnAttachListener {
        void onDetachedFromWindow();
        void onAttachedFromWindow();
    }

    private TypedValue mMinWidthMajor;
    private TypedValue mMinWidthMinor;
    private TypedValue mFixedWidthMajor;
    private TypedValue mFixedWidthMinor;
    private TypedValue mFixedHeightMajor;
    private TypedValue mFixedHeightMinor;

    private final Rect mDecorPadding;
    ...
}

相关文章

网友评论

      本文标题:Activity View 层级 1

      本文链接:https://www.haomeiwen.com/subject/swsbattx.html