Toolbar的基本使用

Posted by Edward on November 9, 2017

一、基本使用

  • 首先要使用没有Actionbar的主题:
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>
    
  • Toolbar本身的标题是居左的,所以一般在其布局下添加TextView作为标题:

<?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:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    app:navigationIcon="@drawable/ic_arrow_left"
    app:theme="@style/Theme.Toolbar">
    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxLines="1"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold" />
</android.support.v7.widget.Toolbar>

其中的 app:navigationIcon=”@drawable/ic_arrow_left” 是设置左侧返回按钮的,也可以设置成其他功能按键。

  • 在Activity中设置Actionbar:
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayShowTitleEnabled(false);
          //左侧返回键监听
          toolbar.setNavigationOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  finish();
              }
          });
    
  • 去掉Toolbar自带的标题 直接设置Toolbar的 android:title=”“,并不能去掉标题,这个设置是无效的。解决办法:
    //方法一:隐藏标题
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    //方法二:设置标题为空
    getSupportActionBar().setTitle("");
    

二、Toolbar中的搜索框样式

    <style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Light">
        <!--搜索框样式-->
        <item name="searchViewStyle">@style/Widget.SearchView</item>
    </style>

    <!--Toolbar中搜索框样式-->
    <style name="Widget.SearchView" parent="Widget.AppCompat.SearchView">
        <!--默认提示-->
        <item name="defaultQueryHint">"请输入关键字"</item>
        <!--搜索图标-->
        <item name="searchIcon">@drawable/ic_search_white_24dp</item>
        <!--关闭图标-->
        <item name="closeIcon">@drawable/ic_close_white_24dp</item>
    </style>

三、Toolbar中的OverflowButton样式

    <style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Light">
        <!-- Toolbar 右侧菜单图标 默认图标是三个点-->
        <item name="actionOverflowButtonStyle">@style/OverFlowIcon</item>
    </style>

    <!--Toolbar中OverflowButton样式-->
    <style name="OverFlowIcon" parent="Widget.AppCompat.ActionButton.Overflow">
        <item name="android:src">@drawable/ic_dehaze_white_24dp</item>
    </style>

四、Toolbar的其他属性

 <style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Light">
        <!-- 菜单文字的颜色 -->
        <item name="actionMenuTextColor">@android:color/white</item>
        <!-- 溢出菜单的文字的颜色 -->
        <item name="android:textColor">@android:color/background_dark</item>
        <!-- 菜单的字体大小-->
        <item name="android:textSize">16sp</item>
        <!--Toolbar文字及图标颜色-->
        <item name="android:textColorPrimary">@color/colorPrimary</item>
        <!--Hint文字颜色-->
        <item name="android:textColorHint">@color/white</item>
        <!--光标颜色-->
        <item name="colorAccent">@color/white</item>
    </style>