Android Toolbar填坑记录

1、相关配置有先后顺序之分

class ToolBarBuilder {
        private Toolbar toolbar;
        /** 标题 */
        private String title;
        /** 是否要隐藏返回键 */
        private boolean hideNavigationIcon = false;
        /** 返回键图标   */
        private int navigationIconId = R.drawable.icon_back;
        /** NavigationIcon点击事件监听(返回键监听)*/
        private View.OnClickListener navigationClickListener;
        /** Menu响应事件监听    */
        private Toolbar.OnMenuItemClickListener menuItemClickListener;

        public ToolBarBuilder setToolbar(Toolbar toolbar){
            this.toolbar = toolbar;
            return this;
        }

        /** 标题 */
        public ToolBarBuilder setTitle(String title){
            this.title = title;
            return this;
        }

        /** 返回键图标设置   */
        public ToolBarBuilder setNavigationIconId(int navigationIconId){
            this.navigationIconId = navigationIconId;
            return this;
        }

        /** NavigationIcon点击事件监听(返回键监听)*/
        public ToolBarBuilder setNavigationClickListener(View.OnClickListener menuItemClickListener){
            this.navigationClickListener = navigationClickListener;
            return this;
        }

        /** Menu Item 点击事件监听(返回键监听)*/
        public ToolBarBuilder setMenuItemClickListener(Toolbar.OnMenuItemClickListener menuItemClickListener){
            this.menuItemClickListener = menuItemClickListener;
            return this;
        }

        /** 是否要隐藏返回键 */
        public ToolBarBuilder setHideNavigationIcon(boolean hideNavigationIcon){
            this.hideNavigationIcon = hideNavigationIcon;
            return this;
        }

        public Toolbar build(){
            if(toolbar != null){
                if(!hideNavigationIcon){// 在不隐藏返回键的情况下要显示返回键
                    toolbar.setNavigationIcon(navigationIconId);
                }
                // 这个属性的配置要写在 setSupportActionBar()之前才会有效果
                toolbar.setTitle(title);
                // 设置返回键监听事件
                if(navigationClickListener == null){
                    navigationClickListener = new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    };
                }
                // 设置Toolbar为导航栏
                setSupportActionBar(toolbar);
                // 这个配置必须写在setSupportActionBar()之后才会有效果。
                toolbar.setNavigationOnClickListener(navigationClickListener);
                // 这个配置必须写在setSupportActionBar()之后才会有效果。
                if(menuItemClickListener != null){
                    toolbar.setOnMenuItemClickListener(menuItemClickListener);
                }
            }
            return toolbar;
        }// end build

    }// end ToolBarBuilder

2、样式设置

<style name="AppTheme" parent="AppTheme.Base">

    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <!--配置toolbar的颜色,但是要在toolbar中进行设置才会起作用-->
        <item name="colorPrimary">@color/bg_toolbar</item>
        <!--配置系统栏的颜色-->
        <item name="colorPrimaryDark">@color/bg_system_bar</item>
        <!--配置整个窗口的背景颜色-->
        <item name="android:windowBackground">@color/bg_window</item>
    </style>
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <!-- android:textColorPrimary is the color of the title text in the Toolbar  -->
        <item name="android:textColorPrimary">@android:color/white</item>
        <!-- actionMenuTextColor is the color of the text of action (menu) items  -->
        <item name="actionMenuTextColor">@android:color/holo_red_dark</item>
        <!-- Tints the input fields like checkboxes and text fields -->
        <!--<item name="colorAccent">@color/cursorAccent</item>-->
        <!-- Applies to views in their normal state. -->
        <!--<item name="colorControlNormal">@color/controlNormal</item>-->
        <!-- Applies to views in their activated state (i.e checked or switches) -->
        <!--<item name="colorControlActivated">@color/controlActivated</item>-->
        <!-- Applied to framework control highlights (i.e ripples or list selectors) -->
        <!--<item name="colorControlHighlight">@color/controlActivated</item>-->

        <!-- Enable these below if you want clicking icons to trigger a ripple effect -->
        <item name="selectableItemBackground">?android:selectableItemBackground</item>
        <item name="selectableItemBackgroundBorderless">?android:selectableItemBackground</item>
    </style>

    <!-- This configures the styles for the title within the Toolbar  -->
    <style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">21sp</item>
        <item name="android:textStyle">italic</item>
    </style>
<?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/android"
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ToolbarTheme"
    app:titleTextAppearance="@style/Toolbar.TitleText"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    >
</android.support.v7.widget.Toolbar>

3、参考https://guides.codepath.com/android/Using-the-App-ToolBar

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容