今天在做一個menu的時候,需要改動item的高度和字體大小顏色。搜索了一下很多方法都不管用,最后是結合了 stack overflow 里面幾個回答的方法勉強實現效果。最近比較忙,先記錄下可以實現的方法,以后再找專門時間好好填坑。
首先對 Android 內部這個彈出menu似乎命名為 OverflowMenu,默認3個點的 more 按鈕應該是命名為OverflowButton,但是部分屬性卻以PopupMenuXXX、PopupXXX、XXXPopup之類格式命名。主要改變item各種屬性的方法是:自定義一個 style,然后在 Toolbar 的 Theme 里使用該 style(暫時叫主 style?),比較麻煩的是,這個主 style 的 item 主要引用其他的 style(暫時叫子 style 吧) ,而部分屬性看似好像某個子 style 的子屬性其實卻有另外專門的 item,另外 子 style 的 parent 繼承對結果也有影響(無效或者出現某些意想不到的效果)。
下面給出一段可以改動以下屬性的代碼,相關解釋直接在注釋里給出好了:
更換彈出menu圖標
彈出時不遮攔Toolbar
Menu item 的高度
Menu item 的背景
Menu item 的字體大小顏色
Menu item 的分隔線
<style name="Toolbar.TechStar" parent="ThemeOverlay.AppCompat.Dark">
<!--修改菜單欄右邊三個白點圖標-->
<item name="android:actionOverflowButtonStyle">@style/TechStarOverflowButtonStyle</item>
<!--ToolBar右邊彈出菜單背景色 不遮攔Toolbar-->
<item name="actionOverflowMenuStyle">@style/techstar.MenuStyle</item>
<!--Menu item 高度 這里不需要指定style 可直接指定-->
<item name="android:listPreferredItemHeightSmall">@dimen/height_45dp</item>
<!-- 分隔線 -->
<item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
<!-- Menu item 文字樣式相關 比較迷的一個點 我自己的項目是留下Large兩個item其1就可以實現 可能和分辨率或者和建造menu時的選項相關???-->
<item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
<item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
</style>
<!--修改菜單欄右邊三個白點圖標-->
<style name="TechStarOverflowButtonStyle"
parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">@drawable/ic_more</item>
</style>
<!--ToolBar右邊彈出菜單背景色 overlapAncho屬性選false可不遮攔Toolbar-->
<style name="techstar.MenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
<item name="overlapAnchor">false</item>
<item name="android:popupBackground">#00FF00</item>
</style>
<!-- 分隔線 -->
<style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
<item name="android:divider">#FF0000</item>
<item name="android:dividerHeight">2dp</item>
</style>
<!-- Menu item 文字樣式 對應item的large或者small 選擇parent 否則無效-->
<style name="myPopupMenuTextAppearanceSmall"
parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
<item name="android:textColor">#FF0000</item>
</style>
<!-- Menu item 文字樣式 對應item的large或者small 選擇parent 否則無效-->
<style name="myPopupMenuTextAppearanceLarge"
parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
<item name="android:textColor">#FF0000</item>
</style>
相關鏈接:
https://gist.github.com/mistrydarshan99/c52ba8345901bcc58442
http://stackoverflow.com/questions/31044370/reduce-menu-items-width-height-and-textview-size