BaseActivity中默认添加ToolBar

1.BaseActivity

public class BaseActivity extends AppCompatActivity{
    private LayoutInflater inflater;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater=LayoutInflater.from(this);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        if(hasToolBar()){
            super.setContentView(R.layout.base);
            FrameLayout frameLayout= (FrameLayout) findViewById(R.id.container);
            inflater.inflate(layoutResID,frameLayout,true);
        }else{
            super.setContentView(layoutResID);
        }
    }
    protected boolean hasToolBar(){
        return true;
    }
}

2.使用

public class MainActivity extends BaseActivity {

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

    @Override
    protected boolean hasToolBar() {//重要  控制是否显示base中的ToolBar
        return true;
    }
}

你可能感兴趣的:(Base中ToBar)