Android5.x Toolbar和Palette应用解析

本文介绍了Android5.x中Toolbar作为Actionbar的升级版的使用方法,包括如何引入、自定义以及与DrawerLayout结合实现侧滑效果。同时,文章详细讲解了Palette的运用,如何通过它提取颜色来实现App主题的动态适应,达到颜色基调的和谐统一。
摘要由CSDN通过智能技术生成

对于已经被不大好用的Actionbar折磨的开发者来说,Toolbar的出现确实是一个好消息,Toolbar是应用的内容的标准工具栏,可以说是Actionbar的升级版,两者不是独立关系,要使用Toolbar还是得跟ActionBar有关系的。相比Actionbar Toolbar最明显的一点就是变得很自由,可随处放置,具体的使用方法和Actionbar很类似。

1. 引入Toolbar
由于用的android studio ,我们首先还是要引入V7支持包,在build.gradle配置如下代码

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:palette-v7:23.0.1'
}

接下来为了显示Toolbar控件,先要将在style里得先把Actionbar去掉

<resources>
    <!-- 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>
</resources>

设置各个部分属性的图
这里写图片描述

接着写一个mytoolbar.xml,引入Toolbar控件,这么写的目的是方便多次调用,我们可以在其他布局中用include引用此布局

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
     >
</android.support.v7.widget.Toolbar>  

在主界面布局用include引用myto

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值