Android ToolBar 的简单封装

使用过 ToolBar 的朋友确定对其使用方法不陌生,由于其用法很简单,若是对 ActionBar 使用比较熟练的人来讲,ToolBar 就更容易了!不过,相信你们在使用的过程当中都遇到过这样一个问题,须要在每个咱们要使用的 xml 中添加 ToolBar 这个控件,好比我须要在 MainActivity中使用 ToolBar,则他的 xml 文件须要这样写,java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <android.support.v7.widget.Toolbar
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:id="@+id/id_tool_bar"
        android:background="?attr/colorPrimary"
        app:navigationIcon="?attr/homeAsUpIndicator"
        >
    </android.support.v7.widget.Toolbar>
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdfasf"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

同理其余 Activity 中须要用页都须要在 xml添加android

<android.support.v7.widget.Toolbar
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:id="@+id/id_tool_bar"
        android:background="?attr/colorPrimary"
        app:navigationIcon="?attr/homeAsUpIndicator"
        >
 </android.support.v7.widget.Toolbar>

这样一段代码,虽然很少,可是咱们最烦的就是写重复代码,也不符合咱们的编程思想;因此就有了如下写法编程

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        />
    <TextView        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdfasf"
        android:layout_alignParentBottom="true"/></RelativeLayout>

toolbar.xml的代码以下app

<?xml version="1.0" encoding="utf-8"?><FrameLayout 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="match_parent">

    <android.support.v7.widget.Toolbar        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:id="@+id/id_tool_bar"
        android:background="?attr/colorPrimary"
        app:navigationIcon="?attr/homeAsUpIndicator"
        >
    </android.support.v7.widget.Toolbar></FrameLayout>

这样咱们只须要在每一个咱们要使用 toolbar 的 xml 中经过 include 嵌入 toolbar.xml布局就行,感受和以前的比,确实是少了几行代码!可是意义不大。而我这里要实现的封装,是能够不须要在 xml 中写一行关于 toolbar 的代码,也就是跟平时不用 toolbar 同样的写法便可!请接着往下看。 
前提是准备好toolbar.xml,ToolBarActivity.java,ToolBarHelper.java 
toolbar.xml中配置 toolbar 的基本属性: 
toolbar 的宽高,toolbar 的背景颜色等其余样式 
ToolBarActivity.java是因此须要使用 toolbar Activity 的父类,这里我把他定义为抽象类,由于单独的这个类不能完成任何功能 
ToolBarHelper.java 是 Activity 和 toolbar 的关联类ide

先来看 toolbar.xml的代码布局

<?xml version="1.0" encoding="utf-8"?><FrameLayout 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="match_parent">
    <android.support.v7.widget.Toolbar        设置高度为 ActionBar 的高度        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:id="@+id/id_tool_bar"
        背景颜色为 ActionBar 的背景颜色        android:background="?attr/colorPrimary"
        返回按钮的图标        app:navigationIcon="?attr/homeAsUpIndicator"
        >
    </android.support.v7.widget.Toolbar></FrameLayout>

ToolBarActivity.java的内容:主要代码是在setContentView(int id) 实现this

package toolbar.toolbar;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.MenuItem;/**
 * Created by moon.zhong on 2015/6/12.
 * time : 10:26
 */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) {

        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);
    }    @Override
    public boolean onOptionsItemSelected(MenuItem item) {        if (item.getItemId() == android.R.id.home){
            finish();            return true ;
        }        return super.onOptionsItemSelected(item);
    }
}

ToolBarHelper.java 
这个类的功能是:先建立一个 ViewGroup 来做为视图的父 View,把用户定义的 View,和 toolBar 依次 Add 到 ViewGroup 中;spa

package toolbar.toolbar;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;/**
 * Created by moon.zhong on 2015/6/12.
 * time : 10:45
 */public class ToolBarHelper {

    /*上下文,建立view的时候须要用到*/
    private Context mContext;    /*base view*/
    private FrameLayout mContentView;    /*用户定义的view*/
    private View mUserView;    /*toolbar*/
    private Toolbar mToolBar;    /*视图构造器*/
    private LayoutInflater mInflater;    /*
    * 两个属性
    * 一、toolbar是否悬浮在窗口之上
    * 二、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.id_tool_bar);
    }    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;
    }
}

到这里,toolbar 的简单封装就算完成了,一块儿来看看封装以后的效果吧code

MainActivity.javaorm

package toolbar.toolbar;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ToolBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);       return true;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <TextView        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdfasf"
        android:layout_alignParentBottom="true"/></RelativeLayout>

到这里咱们无论是 MainActivity 仍是 activity_main中都没有出现 ToolBar,只是 MainActivity 再也不继承 AppCompatActivity,而是继承咱们 ToolBarActivity,运行效果看看: 

ToolBar 的其余用法这里就不讲了,跟 ActionBar 用法几乎同样,

最后: 
在使用 ToolBar 的时候,须要使用无 ActionBar 的主题,

    <!-- Base application theme. -->
    <style name="AppThemeParent" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@android:color/holo_red_light</item>

    </style>

再上一张自定义 View 的 ToolBar 效果图: 
标题居中,右侧能够添加按钮 

源码下载

相关文章
相关标签/搜索