Effect:

Strategy 1: Use native Toolbars for encapsulation

Process:

* 1 All classes inherit this AppToolBarActivity. * 2 Gas cylinder loads a layout into the root layout (that is, layout_toolbar) that contains the ToolBar+Framelayout. * 3getContentView() is an abstract method that returns a View and adds it to framelayoutCopy the code
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_toolbar, (ViewGroup) getWindow().getDecorView().getRootView(), false);
        mContent_frame = view.findViewById(R.id.content_frame);
        if(getContentView() ! = null){ mContent_frame.addView(getContentView()); }setContentView(view);

        ButterKnife.bind(this);
        steepTitle();
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        setTitle(getTitle());

        initWidget();
        initData();
    }
Copy the code
  • Handle the return event for the ToolBar:
/ / style.css. XML < item name ="android:homeAsUpIndicator"> @override public Boolean onOptionsItemSelected(MenuItem item) {if(item.getItemId() == android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
Copy the code
  • Handle intermediate Title overwrite setTitle()
//bar middle title @override public voidsetTitle(CharSequence title) {
        mToolbar_tv_title.setText(title);
        mRadioGroup.setVisibility(View.GONE);
    }   
Copy the code
  • Processing may have text or image buttons on the right and multiple buttons in the middle
// To the right of bar is protected voidsetRightTextButtonEnable(@StringRes int rid, View.OnClickListener onClickListener) { mTv_title_right.setText(rid); mTv_title_right.setOnClickListener(onClickListener); mTv_title_right.setVisibility(View.VISIBLE); } // To the right of the bar is the icon protected voidsetRightImageButtonEnable(@DrawableRes int rid, View.OnClickListener onClickListener) { mIv_title_right.setImageResource(rid); mIv_title_right.setOnClickListener(onClickListener); mIv_title_right.setVisibility(View.VISIBLE); mTv_title_right.setVisibility(View.GONE); } // the middle of bar is multi-button protected voidsetCenterRadioGroupEnable(String btnLefttext,String btnRighttext, RadioGroup.OnCheckedChangeListener onClickListener) {
        mRadioBtnLeft.setText(btnLefttext);
        mRadioBtnRight.setText(btnRighttext);
        mRadioGroup.setOnCheckedChangeListener(onClickListener);
        mRadioGroup.setVisibility(View.VISIBLE);
        mToolbar_tv_title.setVisibility(View.GONE);
    }
    
Copy the code
  • Dealing with what’s called immersion
// Load an immersive status bar public voidsteepTitle() {
        if(Build.VERSION.SDK_INT >= 21) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); / / note to remove FLAG_TRANSLUCENT_STATUS flag getWindow () clearFlags (WindowManager. LayoutParams. FLAG_TRANSLUCENT_STATUS); getWindow().setStatusBarColor(getResources().getColor(R.color.maincolor)); }}Copy the code
  • The final use is super simple:
    @Override
    protected View getContentView() {
        return initRId(R.layout.activity_main);
    }

    @Override
    protected void initWidget() {
        setBackEnable(false);
        setTitle("Home page");
    }
Copy the code

Github address portal

Strategy 2: Write your own Bar and initialize it with an overloaded method

  • Core code:
    protected void initAppBar() {
        initAppBar(true.false, 1, 1); } protected void initAppBar(boolean isBack) { initAppBar(isBack,false, 1, 1); } protected void initAppBar(boolean isBack, boolean isRightText) { initAppBar(isBack, isRightText, -1, -1); } protected void initAppBar(boolean isBack, boolean isRightText, @ColorRes intbgColor, @colorres int textColor) {// Dynamically add appBar so that you do not need to includ the layout of appbar in each XML. // If you do not write these four lines, you need to incloud the layout of bar in each XML getWindow().getDecorView().findViewById(android.R.id.content); ViewGroup inflate = (ViewGroup) view.getChildAt(0); View barView = getLayoutInflater().inflate(R.layout.common_appbar, inflate,false);
        inflate.addView(barView,0);

        RelativeLayout layout = findViewById(R.id.common_appbar_rl);
        if (layout == null) {
            return;
        }
        invadeStatusBar();
        if (bgColor ! = -1) { layout.setBackgroundResource(bgColor);
        }
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            layoutParams.setMargins(0, getStatusBarHeight(), 0, 0);
        } else {
            layoutParams.setMargins(0, 0, 0, 0);
        }
        layout.setLayoutParams(layoutParams);
        ImageView iconIV = findViewById(R.id.common_appbar_iv);
        if(! isBack) { iconIV.setVisibility(View.GONE); }else {
            iconIV.setVisibility(View.VISIBLE);
            iconIV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); }}); } TextView centerTV = findViewById(R.id.common_appbar_center_tv);if(textColor ! = -1) { centerTV.setTextColor(getResources().getColor(textColor)); } centerTV.setText(TypeUtil.isBlank(setAppBarTitle()) ? "" : setAppBarTitle());
        TextView rightTV = findViewById(R.id.common_appbar_right_tv);
        if(! isRightText) { rightTV.setVisibility(View.GONE); }else {
            rightTV.setVisibility(View.VISIBLE);
            if(textColor ! = -1) { centerTV.setTextColor(getResources().getColor(textColor)); } rightTV.setText(TypeUtil.isBlank(setAppBarRightTitle()) ? "" : setAppBarRightTitle());
            rightTV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onAppBarRightClick(); }}); }}Copy the code
  • Overloaded methods:
    protected abstract String setAppBarTitle();

    protected abstract String setAppBarRightTitle();

    protected abstract void onAppBarRightClick();
Copy the code
  • Use:
  initAppBar(true.true);

  @Override
    protected String setAppBarTitle() {
        return "Tactics2";
    }

    @Override
    protected String setAppBarRightTitle() {
        return "More";
    }

    @Override
    protected void onAppBarRightClick() {
        ToastUtil.showToast("More");
    }
Copy the code

Finally, a tip for Amway:

  • When an Activity is jumping, you can write something like this to make it more comfortable:
// Just press start in the target Activity and there will be a built-in method that can pass some data: public static void start(Context context, String id) { Intent starter = new Intent(context, TacticsOneActivity.class); starter.putExtra("id", id); context.startActivity(starter); } // At a button that performs the jump, just say: xxxactivity.start (this,"1");
Copy the code
  • The same Fragment:
// Just type newI: public static ChargeRecordFragment newInstance(int) on the target Fragmenttype,String coinName,String coinDetailName) {
        Bundle args = new Bundle();
        args.putInt("type".type);
        args.putString("coinName",coinName);
        args.putString("coinDetailName",coinDetailName);
        ChargeRecordFragment fragment = new ChargeRecordFragment();
        fragment.setArguments(args);
        returnfragment; } / / somewhere to get the instance of the Activity and transmit the data: ChargeRecordFragment. NewInstance (0, mCoinName, mCoinDetailName);Copy the code