ToolBar的封装主要是利用ViewGroup把自定义的 View一个个加进去。
首先我们先定义一个Toobar.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/accent_material_light"
app:navigationIcon="?attr/homeAsUpIndicator"
android:id="@+id/toobar_id"
/>
主方法入口MainActivity,直接继承ToolBarActivity代码如下:
package com.example.luis.toolbarenc;
import android.os.Bundle;
public class MainActivity extends ToolBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ToolBarActivity是继承AppCompatActivity 并重写setContentView方法,代码如下:
package com.example.luis.toolbarenc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.Window;
/**
* Create by luis on 2016/4/12 18:43
* e-mail:aer874475222@163.com
*/
public abstract class ToolBarActivity extends AppCompatActivity {
private ToolBarHelper mToolBarHelper;
public Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setContentView(int layoutResID) {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
mToolBarHelper = new ToolBarHelper(this, layoutResID);
toolbar = mToolBarHelper.getToolBar();
setContentView(mToolBarHelper.getContentView()); /*把 toolbar 设置到Activity 中*/
setSupportActionBar(toolbar); /*自定义的一些操作*/
onCreateCustomToolBar(toolbar);
}
public void onCreateCustomToolBar(Toolbar toolbar) {
toolbar.setContentInsetsRelative(0, 0);
}a
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
这里我们还要定义ToolBarHelper 这个工具类
package com.example.luis.toolbarenc;
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;
/**
* Create by luis on 2016/4/12 18:44
* e-mail:aer874475222@163.com
*/
public class ToolBarHelper { /*上下文,创建view的时候需要用到*/
private Context mContext; /*base view*/
private FrameLayout mContentView; /*用户定义的view*/
private View mUserView; /*toolbar*/
private Toolbar mToolBar; /*视图构造器*/
private LayoutInflater mInflater; /*
* 两个属性
* 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); /*初始化toolbar*/
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 initToolBar() { /*通过inflater获取toolbar的布局文件*/
View toolbar = mInflater.inflate(R.layout.toolbar, mContentView);
mToolBar = (Toolbar) toolbar.findViewById(R.id.toobar_id);
}
private void initUserView(int id) {
mUserView = mInflater.inflate(id, null);
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);
}
public FrameLayout getContentView() {
return mContentView;
}
public Toolbar getToolBar() {
return mToolBar;
}
}
这样就完成了!!!