效果:
策略一: 使用原生toolbar進(jìn)行封裝
流程:
* 1所有的類都繼承此AppToolBarActivity.
* 2打氣筒加載一個布局到根布局(即layout_toolbar),此布局包含ToolBar+Framelayout.
* 3getContentView()為抽象方法,返回一個View 將此View add到framelayout中.
* 4做一些ToolBar的初始化操作.
* 5使用
@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();
}
- 處理ToolBar的返回事件:
//style.xml中
<item name="android:homeAsUpIndicator">@mipmap/back_white</item>
//AppToolBarActivity中
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
- 處理中間Title 重寫setTitle()
//bar 中間標(biāo)題
@Override
public void setTitle(CharSequence title) {
mToolbar_tv_title.setText(title);
mRadioGroup.setVisibility(View.GONE);
}
- 處理右側(cè)可能是文字或圖片按鈕 中間可能是多按鈕
//bar右側(cè)是文字
protected void setRightTextButtonEnable(@StringRes int rid, View.OnClickListener onClickListener) {
mTv_title_right.setText(rid);
mTv_title_right.setOnClickListener(onClickListener);
mTv_title_right.setVisibility(View.VISIBLE);
}
//bar右側(cè)是圖標(biāo)
protected void setRightImageButtonEnable(@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);
}
//bar中間是多按鈕
protected void setCenterRadioGroupEnable(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);
}
- 處理所謂的沉浸式
//加載沉浸式狀態(tài)欄
public void steepTitle() {
if (Build.VERSION.SDK_INT >= 21) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//注意要清除 FLAG_TRANSLUCENT_STATUS flag
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(getResources().getColor(R.color.maincolor));
}
}
- 最后使用超簡單:
@Override
protected View getContentView() {
return initRId(R.layout.activity_main);
}
@Override
protected void initWidget() {
setBackEnable(false);
setTitle("主頁");
}
策略2:自己寫一個Bar 使用重載方法初始化
- 核心代碼:
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 int bgColor, @ColorRes int textColor) {
//動態(tài)添加appbar 這樣無需在每個xml中includ進(jìn)去appbar的布局
//如果不寫這四行 需要在每個xml中incloud進(jìn)去bar布局
ViewGroup view = 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();
}
});
}
}
- 重載方法:
protected abstract String setAppBarTitle();
protected abstract String setAppBarRightTitle();
protected abstract void onAppBarRightClick();
- 使用:
initAppBar(true,true);
@Override
protected String setAppBarTitle() {
return "Tactics2";
}
@Override
protected String setAppBarRightTitle() {
return "更多";
}
@Override
protected void onAppBarRightClick() {
ToastUtil.showToast("更多");
}
最后安利一個小技巧:
- Activity跳轉(zhuǎn)時候,可以這么寫,更舒服:
//只需要在目標(biāo)Activity敲下start 就會出現(xiàn)自帶的方法,可以傳遞一些數(shù)據(jù):
public static void start(Context context, String id) {
Intent starter = new Intent(context, TacticsOneActivity.class);
starter.putExtra("id", id);
context.startActivity(starter);
}
//在執(zhí)行跳轉(zhuǎn)的某按鈕處,只需簡短一句:
XxxActivity.start(this,"1");
- 同理Fragment:
//只需要在目標(biāo)Fragment敲下newI :
public static ChargeRecordFragment newInstance(int type,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);
return fragment;
}
//Activity的某處拿到實例并傳遞數(shù)據(jù):
ChargeRecordFragment.newInstance(0,mCoinName,mCoinDetailName);