public abstract class BaseActivity extends AppCompatActivity{ //the container of this activity layout and sub-activity layout privateLinearLayout parentLinearLayout;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContentView(R.layout.activity_base); setContentView(getLayoutId()); initToolBar(); init(); } // overwrite the function in sub-activity and return the layout id of sub-activity protected abstract int getLayoutId();
private void initContentView(@LayoutRes int layoutResID) { ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content); viewGroup.removeAllViews(); parentLinearLayout = new LinearLayout(this); parentLinearLayout.setOrientation(LinearLayout.VERTICAL); // add parentLinearLayout in viewGroup viewGroup.addView(parentLinearLayout); // add the layout of BaseActivity in parentLinearLayout LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true); } /** * @param layoutResID layout id of sub-activity */ @Override public void setContentView(@LayoutRes int layoutResID) { // added the sub-activity layout id in parentLinearLayout LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true);
public abstract class BaseActivity extends AppCompatActivity{ //the container of this activity layout and sub-activity layout private LinearLayout parentLinearLayout; private TextView mTvTitle; private TextView mTvRight; private Toolbar mToolbar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContentView(R.layout.activity_base);
setContentView(getLayoutId()); initToolBar(); setBackIcon(); init(); } // overwrite the function in sub-activity and return the layout id of sub-activity protected abstract int getLayoutId();
private void initContentView(@LayoutRes intlayoutResID) { ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content); viewGroup.removeAllViews(); parentLinearLayout = newLinearLayout(this); parentLinearLayout.setOrientation(LinearLayout.VERTICAL); viewGroup.addView(parentLinearLayout); // add the layout of BaseActivity in parentLinearLayout LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true); } /** * @param layoutResID the layout id of sub Activity */ @Override public void setContentView(@LayoutRes intlayoutResID) { // added the sub-activity layout id in parentLinearLayout LayoutInflater.from(this).inflate(layoutResID, parentLinearLayout, true);
/** * @return TextView in center */ public TextView getToolbarTitle() { return mTvTitle; }
/** * @return TextView on the right */ public TextView getSubTitle() { return mTvRight; }
/** * set Title * @param title */ public void setToolBarTitle(CharSequence title) { if (mTvTitle != null) { mTvTitle.setText(title); } else { getToolbar().setTitle(title); setSupportActionBar(getToolbar()); } }
/** * the toolbar of this Activity * @return support.v7.widget.Toolbar. */ public Toolbar getToolbar() { return (Toolbar) findViewById(R.id.toolbar); }
/** * is show back icon,default is none。 * you can override the function in subclass and return to true show the back icon * @return */ protected boolean isShowBacking() { return true; } }