Toolbar笔记1

来源:互联网 发布:c语言预处理功能 编辑:程序博客网 时间:2024/05/13 13:24

Toolbar使用笔记

首先,Toolbar使用需要将主题改为noactionbar的形式

<style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">        <item name="colorPrimary">@color/blue</item>        <item name="colorPrimaryDark">@color/ripple_material_light</item>        <item name="android:windowBackground">@color/dim_foreground_material_dark</item>    </style>
就是这种形式了,其余的这里面设置了几种颜色,方便后面用的时候引用,

<pre name="code" class="html">colorPrimary//Toolbar的背景颜色
<pre name="code" class="html">colorPrimaryDark//状态栏的颜色,手机版本低的不能实现,具体的我也没有测试,因为直接下载个demo来实现状态栏变色都无法实现,暂时认为我的手机的问题吧(酷派大神。。。)
android:windowBackground//主要界面内的背景颜色

然后设置布局文件
  <android.support.v7.widget.Toolbar        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>
android:background="?attr/colorPrimary"     引用前面主题设置好的颜色
<pre name="code" class="html">android:minHeight="?attr/actionBarSize" >    设置Toolbar的最小高度

这个时候需要将创建的Activity继承至AppCompatActivity,其余代码都一样,

<span style="white-space:pre"></span>// 对Toolbar进行赋值<span style="white-space:pre"></span>toolbar = (Toolbar) findViewById(R.id.toolbar);<span style="white-space:pre"></span>// 将Toolbar设置为actionbar<span style="white-space:pre"></span>setSupportActionBar(toolbar);<span style="white-space:pre"></span>// 设置logo显示在Toolbar上<span style="white-space:pre"></span>toolbar.setLogo(R.drawable.ic_launcher);<span style="white-space:pre"></span>// 设置主标题<span style="white-space:pre"></span>toolbar.setTitle("My Title");<span style="white-space:pre"></span>// 设置副标题<span style="white-space:pre"></span>toolbar.setSubtitle("Sub title");<span style="white-space:pre"></span>// 添加点击事件监听<span style="white-space:pre"></span>toolbar.setOnMenuItemClickListener(onMenuItemClick);<span style="white-space:pre"></span>// 给左上角图标的左边加上一个返回的图标 。<span style="white-space:pre"></span>getSupportActionBar().setDisplayHomeAsUpEnabled(true);<span style="white-space:pre"></span>// 声明一个drawelayout<span style="white-space:pre"></span>mDrawerLayout = (DrawerLayout) findViewById(R.id.news_activity);<span style="white-space:pre"></span>// ActionBarDrawerToggle 是<span style="white-space:pre"></span>// DrawerLayout.DrawerListener实现类,重写onDrawerSlide该方法是<span style="white-space:pre"></span>// 为了监听重提是否打开,保证打开时back键是关闭抽屉而不是退出应用<span style="white-space:pre"></span>// 同时该方法保证Toolbar上按钮的动画效果随着抽屉移动量改变<span style="white-space:pre"></span>mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.id.action_bar, R.id.action_bar) {<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void onDrawerSlide(View drawerView, float slideOffset) {<span style="white-space:pre"></span>if (slideOffset == 0) {<span style="white-space:pre"></span>isOpen = false;<span style="white-space:pre"></span>} else {<span style="white-space:pre"></span>isOpen = true;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>super.onDrawerSlide(drawerView, slideOffset);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>};<span style="white-space:pre"></span>// 该方法会自动和actionBar关联, 将开关的图片显示在了action上,如果<span style="white-space:pre"></span>// 不设置,也可以有抽屉的效果,不过是默认的图标<span style="white-space:pre"></span>mDrawerToggle.syncState();<span style="white-space:pre"></span>// 为DrawerLayout添加监听器<span style="white-space:pre"></span>mDrawerLayout.setDrawerListener(mDrawerToggle);
上面代码大致说名了各个位置设置内容的方法。

然后就是引入菜单

菜单需要在res/menu/中新建一个xml文件,用已有的也可以,还以我做的为例

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.example.anytime.NewsActivity" >    <item        android:id="@+id/action_notify"        android:icon="@drawable/notification"        android:orderInCategory="80"        android:title="action_search"        app:showAsAction="ifRoom"/>    <item        android:id="@+id/action_theme_settings"        android:orderInCategory="100"        android:title="?attr/menucontent"        app:showAsAction="never"/>    <item        android:id="@+id/action_setting"        android:orderInCategory="100"        android:title="action_settings"        app:showAsAction="never"/></menu>
首先可以将上面的每一个item理解为菜单的一项,id不用说,icon是图标,orderInCategory为优先级,值越大优先级越小,title为显示的文字内容,showAsAction最为重要,这个有五种属性分别是:

ifRoom        会以图标的形式显示在Item中,但是如果已经有优先级高的将位置占满则隐藏在溢出列表中。

never         永远不会显示。只会在溢出列表中显示,不显示图标,只显示标题,所以在定义item的时候,最好把标题都带上。    

always      无论是否溢出,总会显示。 

withText    withText值示意Toolbar要显示文本标题。Toolbar会尽可能的显示这个标题,但是,如果图标有效并且受到Toolbar空间的限制,文本标题有可能显示不全。    

collapseActionView      声明了这个操作视窗应该被折叠到一个按钮中,当用户选择这个按钮时,这个操作视窗展开。否则,这个操作视窗在默认的情况下是可见的,并且即便在用于不适用的时候,也要占据操作栏的有效空间。一般要配合ifRoom一起使用才会有效果。

0 0
原创粉丝点击