http://liuwangshu.cn/application/view/6-activity-constitute.html (刘望舒)

所以,了解了上面的activity的构成之后,有哪些用途呢?

实用场景1

最近在做一款车机应用,但是系统要求应用适配左边的一个侧边栏。这样的话,那我每个activity都得在根布局左边设置一个和侧边栏宽度一样的margin值。

于是,我在BaseActivity里把将原有的contentLayout取到,然后创建一个和侧边样一样大小的view与原有的contentLayout形成一个线性布局再加到decorView里解决问题:

 private void relayoutContent() {
        //已经执行过一次,不重复执行。
        if (contentParent != null) {
            return;
        }
        //调整应用布局的留白
        View contentView = getContentView(this);
        //ContentFrameLayout
        contentParent = (ViewGroup) contentView.getParent();
        LogUtil.d(TAG, "contentParent " + contentParent);
        contentParent.removeAllViews();

        //创建一个LinearLayout装留白和contentView
        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams linearLayoutLP = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(linearLayoutLP);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        NavigationPlaceHolderView navigationPlaceHolderView = new NavigationPlaceHolderView(this);
        LinearLayout.LayoutParams navigationPlaceHolderViewLP = new LinearLayout.LayoutParams(145, ViewGroup.LayoutParams.MATCH_PARENT);
        navigationPlaceHolderViewLP.width = 145;
        navigationPlaceHolderViewLP.height = ViewGroup.LayoutParams.MATCH_PARENT;
        navigationPlaceHolderView.setLayoutParams(navigationPlaceHolderViewLP);
        navigationPlaceHolderView.setId(VIEW_ID_NAVIGATION_PLACE_HOLDER_VIEW);
        linearLayout.addView(navigationPlaceHolderView);

        LinearLayout.LayoutParams contentLP = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        contentLP.weight = 1;
        contentView.setLayoutParams(contentLP);
        linearLayout.addView(contentView);

        contentParent.addView(linearLayout);

    }
分类: view体系

0 条评论

发表回复

您的电子邮箱地址不会被公开。