android 代码为activity设置contentview

Activity:

场景:产品要求所有页面统一换一个背景

思路:在ActivityBase中,代码创建ContentView 然后往ContentView 添加一个imageview 然后通过 abstract protected int getContentViewLayoutID(); 添加子类自己的布局

代码如下:

// 设置view
            FrameLayout contentView = new FrameLayout(this);
            {
                ImageView bgImage = new ImageView(this);
                bgImage.setImageResource(R.mipmap.controller_bg);
                bgImage.setScaleType(ImageView.ScaleType.FIT_XY);
                bgImage.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                contentView.addView(bgImage);
            }
            {
                View customView = LayoutInflater.from(this).inflate(getContentViewLayoutID(),null);
                customView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                contentView.addView(customView);
            }
            setContentView(contentView);
            ButterKnife.bind(this);
    abstract protected int getContentViewLayoutID();

Frament:

场景:产品要求所有页面统一换一个背景

思路:在FragmentBase中,代码创建ContentView 然后往ContentView 添加一个imageview 然后添加子类自己的布局

代码如下:

FrameLayout contentView = new FrameLayout(inflater.getContext());
        {
            ImageView bgImage = new ImageView(getContext());
            bgImage.setImageResource(R.mipmap.controller_bg);
            bgImage.setScaleType(ImageView.ScaleType.FIT_XY);
            bgImage.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            contentView.addView(bgImage);
        }
        {
            View customView = inflater.inflate(getContentViewLayoutID(),null);
            customView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            contentView.addView(customView);
            ButterKnife.bind(this,customView);
        }
        return contentView;

记个笔记

//  Fragment 添加布局
View view = inflater.inflate(getContentViewLayoutID(),container,false);
ButterKnife.bind(this,view);
return view;
// activity
setContentView(getContentViewLayoutID());

你可能感兴趣的:(android 代码为activity设置contentview)