效果:
执行效果
策略一: 使用原生toolbar进行封装
流程:
* 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 中间标题
@Override
public void setTitle(CharSequence title) {
mToolbar_tv_title.setText(title);
mRadioGroup.setVisibility(View.GONE);
}
- 处理右侧可能是文字或图片按钮 中间可能是多按钮
//bar右侧是文字
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右侧是图标
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);
}
- 处理所谓的沉浸式
//加载沉浸式状态栏
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) {
//动态添加appbar 这样无需在每个xml中includ进去appbar的布局
//如果不写这四行 需要在每个xml中incloud进去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跳转时候,可以这么写,更舒服:
//只需要在目标Activity敲下start 就会出现自带的方法,可以传递一些数据:
public static void start(Context context, String id) {
Intent starter = new Intent(context, TacticsOneActivity.class);
starter.putExtra("id", id);
context.startActivity(starter);
}
//在执行跳转的某按钮处,只需简短一句:
XxxActivity.start(this,"1");
- 同理Fragment:
//只需要在目标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的某处拿到实例并传递数据:
ChargeRecordFragment.newInstance(0,mCoinName,mCoinDetailName);
网友评论