Android中的菜單顯示風格

來源:互聯網
上載者:User

田海立

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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.