Activity View 层级 1

通过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如下:




    


这里的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)