http://blog.csdn.net/yingtian648/article/details/52432438(转载)

1.在Toolbar.xml中设置弹出菜单的风格(app:popupTheme =@style/OverflowMenuStyle)

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
android:background 设置ToolBar背景色
android:titleTextColor 设置字体颜色
app:navigationIcon 设置左边按钮图标
app:popupTheme 设置弹出菜单的风格
-->
<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="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:navigationIcon="@drawable/back"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/OverflowMenuStyle"
>
</android.support.v7.widget.Toolbar>

2.values/styles.xml下添加弹出菜单样式(需要先添加到appTheme中)

 styles.xml

<resources>

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- 加入toolbar溢出【弹出】菜单的风格 -->
<item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item>
</style> <!--溢出[弹出]菜单样式 parent相当于继承【可以不要parent】-->
<style name="OverflowMenuStyle" parent="@style/Widget.AppCompat.PopupMenu.Overflow">
<!-- 是否覆盖锚点,默认为true,即盖住Toolbar -->
<item name="overlapAnchor">false</item>
<item name="android:dropDownWidth">wrap_content</item>
<item name="android:paddingRight">5dp</item>
<!-- 弹出层背景颜色 -->
<item name="android:popupBackground">@color/colorPrimaryDark</item>
<!-- 弹出层垂直方向上的偏移,即在竖直方向上距离Toolbar的距离,值为负则会盖住Toolbar -->
<item name="android:dropDownVerticalOffset">5dp</item>
<!-- 弹出层水平方向上的偏移,即距离屏幕左边的距离,负值会导致右边出现空隙 -->
<item name="android:dropDownHorizontalOffset">0dp</item>
<!-- 设置弹出菜单文字颜色 -->
<item name="android:textColor">@color/colorPrimary</item>
</style> </resources>

3.在res下创建(Android resource directory ),类型选择为“menu ”,名字可以使用默认名字“menu”,在新建的menu目录下创建“Menu resource file”,命名为toolbar

menu下toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/other"
android:icon="@drawable/other"
android:title="Other"
app:showAsAction="always"
>
<!-- item包含menu表示该menu是item下的子菜单 -->
<menu>
<!-- 将group中的菜单项放于一个组里面 -->
<group>
<item android:id="@+id/toolbar_r_1"
android:title="吃饭"
android:icon="@drawable/eat"
app:showAsAction="ifRoom"
/>
<item android:id="@+id/toolbar_r_2"
android:title="睡觉"
android:icon="@drawable/sleep"
app:showAsAction="ifRoom"
/>
<item android:id="@+id/toolbar_r_3"
android:title="打豆豆"
android:icon="@drawable/dadoudou"
app:showAsAction="ifRoom"
/>
</group>
</menu>
</item>
</menu>

4.mainactivty对toolbar操作同toolbar+drawlayout

请看

最新文章

  1. linux vi(vim)常用命令汇总
  2. MVC文件上传文件限制
  3. 【转】字符集和字符编码(Charset &amp; Encoding)
  4. 昂贵的聘礼(poj 1062)
  5. Android拍照、录像、录音代码范例
  6. MVC controller and View
  7. .nil? .empty? .blank? .present? in Ruby on Rails
  8. poj 1328 Radar Installation(nyoj 287 Radar):贪心
  9. javascript学习代码
  10. Spark学习笔记--stage和task的划分
  11. 修改IE8搜索框为指定搜索引擎,如CSDN、百度知道等
  12. bzoj 1020-1029
  13. 在 Xshell 中 使用 hbase shell 进入后 无法删除
  14. django drf 基础学习4
  15. Exp2 后门原理与实践 20154320 李超
  16. 通过jquery隐藏和显示元素
  17. Expo大作战(四十)--expo sdk api之 Calendar,Constants
  18. ASP.NET RAZOR自动生成的js Timer
  19. 产生渐变色的view
  20. NYOJ 117 求逆序数 (树状数组)

热门文章

  1. [Luogu P2891/POJ 3281/USACO07OPEN ]吃饭Dining
  2. MarkDown排版、多样的文本框
  3. How to get last SysExcelWorksheet object row or column[X++]
  4. 一看就懂的MySQL的FreeList机制
  5. Mysql事物与二阶段提交
  6. mds的cpu占用问题分析以及解决办法
  7. du查看的目录大小与df查看的大小不同的时候用lsof查找
  8. mysql 创建数据库知识总结
  9. Python_图解教程
  10. Linux内核源码分析之set_arch (一)