Material Design之ToolBar

ToolBar可以定制修改的地方有:

  1.     设置导航栏图标
  2.     设置App的logo
  3.     支持设置标题和子标题
  4.     支持添加一个或者多个的自定义控件
  5.     支持Action Menu


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_tl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_blue">

        <!--自定义控件-->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义控件"
            android:textColor="@android:color/white"
            android:textSize="@dimen/middle" />
    </android.support.v7.widget.Toolbar>
</LinearLayout>

Action Menu 布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/up"
        android:icon="@mipmap/up"
        android:title="提交"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/share"
        android:icon="@mipmap/wd_icon_fx"
        android:title="分享"
        app:showAsAction="ifRoom" />
</menu>

然后在java代码中获取ToolBar,设置相应的值。如下:

toolbar_tl= (Toolbar) findViewById(R.id.toolbar_tl);
toolbar_tl.setNavigationIcon(R.mipmap.lst_sy_ptsk); //设置导航栏图标
toolbar_tl.setLogo(R.mipmap.ic_launcher);//设置logo
toolbar_tl.setTitle("Title");//设置标题
toolbar_tl.setSubtitle("subtitle");//设置子标题
toolbar_tl.inflateMenu(R.menu.boolbar_menu); //设置右上角填充菜单
//设置菜单点击事件
toolbar_tl.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.up:
                Toast.makeText(mContext,"您点击了up",Toast.LENGTH_SHORT).show();
                break;
            case R.id.share:
                Toast.makeText(mContext,"您点击了share",Toast.LENGTH_SHORT).show();
                break;
        }
        return false;
    }
});

猜你喜欢

转载自blog.csdn.net/zouzoutingting_/article/details/76635676