田海立

2012-9-13

本文以IconMenu为例,看菜单显示风格。根据需求替换或更改相应的资源就可以定制菜单的风格。

Keywords: Menu MenuItem Theme Style Color

一、Menu风格相关的实现

MenuBuilder(frameworks/base/core/java/com/android/internal/view/menu/MenuBuilder.java)中定义Menu的类型以及相应的ThemeLayout等信息。

这里Menu的类型是从实现的角度看的分类,不用于应用开发者所熟知的ContextMenu、OptionsMenu的分类。

不同类型Menu对应Theme的定义:

    static final intTHEME_RES_FOR_TYPE[] = new int[] {       com.android.internal.R.style.Theme_IconMenu,       com.android.internal.R.style.Theme_ExpandedMenu,        0,    };

对于IconMenu,其Theme Theme.IconMenu定义在frameworks/base/core/res/res/values/themes.xml

    <style name="Theme.IconMenu">        <!--Menu/item attributes -->        <item name="android:itemTextAppearance">@android:style/TextAppearance.Widget.IconMenu.Item</item>        <item name="android:itemBackground">@android:drawable/menu_selector</item>        <item name="android:itemIconDisabledAlpha">?android:attr/disabledAlpha</item>        <item name="android:horizontalDivider">@android:drawable/divider_horizontal_dark</item>        <item name="android:verticalDivider">@android:drawable/divider_vertical_dark</item>        <item name="android:windowAnimationStyle">@android:style/Animation.OptionsPanel</item>        <item name="android:moreIcon">@android:drawable/ic_menu_more</item>        <item name="android:background">@null</item>    </style>

二、各设置项的定义

2.1 背景

默认是没有背景的

2.2 子菜单背景

每个MenuItem背景的定义通过“android:itemBackground”指向menu_selector,menu_selector定义在frameworks/base/core/res/res/drawable/menu_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item       android:state_pressed="true"       android:state_enabled="true"       android:drawable="@drawable/highlight_pressed" />    <item       android:state_selected="true"       android:state_enabled="false"        android:drawable="@drawable/highlight_disabled"/>    <item       android:state_selected="true"       android:state_enabled="true"       android:drawable="@drawable/highlight_selected" />    <item       android:state_focused="true"       android:state_enabled="false"       android:drawable="@drawable/highlight_disabled" />    <item       android:state_focused="true"       android:state_enabled="true"       android:drawable="@drawable/highlight_selected" /></selector>

不同MenuItem在状态下显示的图片是highlight_xxx.png。这些图片放在frameworks/base/core/res/res/drawable-<dpi>/下。

2.3 子菜单文本显示

每个MenuItem文本显示风格的定义通过“android:itemTextAppearance”指向@android:style/TextAppearance.Widget.IconMenu.Item。TextAppearance.Widget.IconMenu.Item定义在frameworks/base/core/res/res/values/styles.xml

    <style name="TextAppearance.Widget.IconMenu.Item" parent="TextAppearance.Small">        <item name="android:textColor">?textColorPrimary</item>    </style>

?textColorPrimary定义在是frameworks/base/core/res/res/values/themes.xml

    <style name="Theme">        <item name="textColorPrimary">@android:color/primary_text_dark</item>    </style>

primary_text_dark定义在frameworks/base/core/res/res/color/primary_text_dark.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_enabled="false" android:color="@android:color/bright_foreground_dark_disabled"/>    <item android:state_window_focused="false" android:color="@android:color/bright_foreground_dark"/>    <item android:state_pressed="true" android:color="@android:color/bright_foreground_dark_inverse"/>    <item android:state_selected="true" android:color="@android:color/bright_foreground_dark_inverse"/>    <item android:state_focused="true" android:color="@android:color/bright_foreground_dark_inverse"/>    <item android:color="@android:color/bright_foreground_dark"/> <!--not selected --></selector>

@android:color/<color>都定义在frameworks/base/core/res/res/values/colors.xml

    <color name="bright_foreground_dark">#ffffffff</color>    <color name="bright_foreground_dark_disabled">#80ffffff</color>    <color name="bright_foreground_dark_inverse">#ff000000</color>


更多相关文章

  1. android 自定义ripple
  2. Android 自定义标签属性设置及使用
  3. android中popupwindow弹出后,屏幕背景变成半透明
  4. android v7 sample ActionBarDisplayOptions没有显示自定义logo
  5. Android 5.0 自定义dialog 背景不透明解决方法
  6. 【Android】_UI设计_菜单导航
  7. android 自定义xml属性

随机推荐

  1. android中处理图片成圆角
  2. Android 解析字符乱码解决
  3. Android 添加系统设置属性
  4. Android AsyncTask 源码解析
  5. Android之Hello World
  6. 使用arm-eabi-addr2line, ndk-stack工具
  7. android错误:Installation error: INSTALL
  8. Android(安卓)有两种办法可以监控SDCARD
  9. Android RelativeLayout和LinearLayout性
  10. Android 网络管理