实现toolbar的标题居中,更改返回按钮图片,动态设置textview中的图片,
添加点击事件,关闭原始title,添加状态栏颜色。
核心代码:
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
/**
* Created by haoyan on 2016/3/15.
*/
public class ToolBarHelper {
private Context mContext;
private FrameLayout mContentView;
//自己定义
private View mUserView;
private Toolbar mToolBar;
private LayoutInflater mInflater;
private TextView mtv_center;
private TextView mtv_right;
//1.toolbar是否悬浮在窗口之上
//2.toolbar的高度
private static int[] ATTRS={
R.attr.windowActionBarOverlay,
R.attr.actionBarSize
};
public ToolBarHelper(Context context,int layoutId){
this.mContext=context;
mInflater=LayoutInflater.from(mContext);
//初始化内容
initContentView();
//初始化用户布局
initUserView(layoutId);
initToolBar();
}
private void initContentView(){
//创建布局作为容器
mContentView =new FrameLayout(mContext);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT );
mContentView.setLayoutParams(params);
}
private void initUserView(int layoutId) {
mUserView=mInflater.inflate(layoutId,null);
//LayoutParams相当于命令传递者
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
//设置自定义属性到控件中
TypedArray typedArray =mContext.getTheme().obtainStyledAttributes(ATTRS);
//获取主题中定义的悬浮标志
boolean overly = typedArray.getBoolean(0,false);
//获取主题中定义的toolbar的高度
int toolBarSize=(int) typedArray.getDimension(1,(int)mContext.getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
typedArray.recycle();
//如果是悬浮状态,则不需要设置间距
params.topMargin=overly?0:toolBarSize;
mContentView.addView(mUserView, params);
}
private void initToolBar(){
View toolbar=mInflater.inflate(R.layout.toolbar,mContentView);
mToolBar= (Toolbar) toolbar.findViewById(R.id.fl_toolbar);
mtv_center=(TextView)toolbar.findViewById(R.id.tv_center);
mtv_right=(TextView)toolbar.findViewById(R.id.tv_right);
}
public FrameLayout getContentView(){
return mContentView;
}
public Toolbar getToolBar(){
return mToolBar;
}
//中间标题
public TextView getTvCenter(){
return mtv_center;
}
//右侧标题
public TextView getTvRight(){
return mtv_right;
}
}
以下代码用来设置到自定义的BaseActivity或BaseFragment中:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
/**
* Created by haoyan on 2016/3/15.
*/
public abstract class ToolBarActivity extends AppCompatActivity implements View.OnClickListener{
private ToolBarHelper mToolBarHelper;
public Toolbar toolbar;
public TextView tv_center;
public TextView tv_right;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setContentView(int layoutResID) {
mToolBarHelper=new ToolBarHelper(this,layoutResID);
toolbar = mToolBarHelper.getToolBar();
toolbar.setTitle("");
tv_center= mToolBarHelper.getTvCenter();
tv_right=mToolBarHelper.getTvRight();
tv_right.setOnClickListener(this);
//返回帧布局视图
setContentView(mToolBarHelper.getContentView());
setSupportActionBar(toolbar);//把toolbar设置到activity中
onCreateCustomToolBar(toolbar);
}
public void onCreateCustomToolBar(Toolbar toolbar) {
//插入toolbar视图的内容的起始点与结束点
toolbar.setContentInsetsRelative(0,0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
基础布局调用代码:
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ToolBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//此部分为基本用法
// Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
// toolbar.setTitle("崭新的键盘");
// toolbar.setSubtitle("多谢各位");
// setSupportActionBar(toolbar);
// toolbar.setNavigationIcon(R.mipmap.ic_launcher);
}
public void onCreateCustomToolBar(Toolbar toolbar){
super.onCreateCustomToolBar(toolbar);
tv_center.setText("新标题");
//设置图片
Drawable myImage = MainActivity.this.getResources().getDrawable(R.mipmap.ic_launcher);
tv_right.setCompoundDrawablesWithIntrinsicBounds(myImage,null, null, null);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_right:
Toast.makeText(this,"哈喽", Toast.LENGTH_LONG).show();
break;
default:
}
}
}
布局文件代码:app:navigationIcon="?attr/homeAsUpIndicator">此处调用更改返回按钮图片
清单文件代码: