Material Design之Toolbar的使用以及修改菜单和字体颜色

来源:互联网 发布:dbc网络用语 编辑:程序博客网 时间:2024/06/05 06:02

Toolbar简介

Toolbar使应用的标准工具栏,可以说是Actionbar的升级版。和Actionbar相比,Toolbar最明显的变化是自由,方便定制。

Toolbar的使用

因为只要有Toolbar,所以需要把原本的 ActionBar 隐藏起来。在style文件设定主题

    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>

布局文件

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/toolbar"    android:background="?attr/colorPrimary"    android:layout_width="match_parent"    android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>

再来添加一个抽屉效果

<?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:orientation="vertical">    <include layout="@layout/include_toolbar"/>    <android.support.v4.widget.DrawerLayout        android:id="@+id/main_drawlayout"        android:layout_width="match_parent"        android:layout_height="match_parent">        <!-- menu部分-->        <FrameLayout            android:layout_width="180dp"            android:layout_height="match_parent"            android:background="#72f076"            android:layout_gravity="left"></FrameLayout>        <!-- 内容部分-->        <FrameLayout            android:layout_width="180dp"            android:layout_height="match_parent"            android:background="#6a706a"></FrameLayout>    </android.support.v4.widget.DrawerLayout></LinearLayout>

然后在我们的Activity中初始化即可:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.main_drawlayout);        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);        drawerLayout.setDrawerListener(toggle);        //同步状态        toggle.syncState();

让我们来瞅瞅效果

这里写图片描述
等等,这丑暴了的标题颜色是怎么回事(背景颜色请无视。。)
我们来修改下修改Toolbar文字颜色和修改Toolbar popup menu样式
在AppBase中添加一行代码

    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>        <item name="android:textColorPrimary">@android:color/white</item>    </style>

再添加一个菜单样式

    <!-- toolbar菜单样式 -->    <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">        <item name="actionMenuTextColor">@android:color/white</item>    </style>

给toolbar即可

app:theme="@style/ToolbarTheme"

更多资料:
Android Toolbar样式定制详解

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 吃母乳的宝宝不爱喝水怎么办 三个月宝宝不肯吃奶粉怎么办 三个月宝宝不肯喝奶粉怎么办 三个月的宝宝不肯喝奶粉怎么办 三个月的宝宝不肯吃奶粉怎么办 饿了还是没食欲怎么办 3岁半幼儿便秘怎么办 小孩字写得难看怎么办 小孩的字写的丑怎么办 小孩字写的太差怎么办 小孩很多字不会写怎么办 食欲不好吃不多怎么办 中班小孩子子不肯写字怎么办 1岁宝便秘该怎么办 两周的宝宝便秘怎么办 3岁宝宝不肯吃药怎么办 1岁婴儿不肯吃药怎么办 9月婴儿不肯吃药怎么办 半岁宝宝不吃药怎么办 1岁宝宝不爱吃药怎么办 2岁吃多了呕吐怎么办啊 小孩吃撑了吐怎么办 三岁宝宝吃太多怎么办 小孩吃撑发烧了怎么办 婆婆不帮忙带孩子怎么办 孩子在中班幼儿园不爱写字怎么办 小孩不愿意上学不愿意写字怎么办 宝妈太内向不喜欢带孩子出门怎么办 宝宝吃饭不带围兜怎么办 宝宝吃饭不喜欢带围兜怎么办 宝宝吃饭不爱带围兜怎么办 孩子不吃肉和菜怎么办 我很恶心我婆婆怎么办 坐月子不喜欢婆婆抱宝宝怎么办 宝宝一写字就哭怎么办 4岁宝宝不爱学习怎么办 4岁宝宝不爱写字怎么办 孩子调皮老师不让上学了怎么办 小学生写字握笔握出剪子来怎么办 儿子6岁不会写字怎么办 宝宝在幼儿园不说话怎么办