如何更改android中弹出菜单的文字颜色和大小?

时间:2014-07-30 12:56:45

标签: android popupmenu

即使在样式中更改,弹出菜单中的文本颜色也不会改变。 背景颜色相对于styles.xml中的颜色正在变化,但文本颜色和文本大小没有反映。

//Creating the instance of PopupMenu  
PopupMenu popup = new PopupMenu(mContext, holder.im_overflow);      
//Inflating the Popup using xml file  
popup.getMenuInflater().inflate(R.menu.list_overflow_menu, popup.getMenu());     
//registering popup with OnMenuItemClickListener  
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {      
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if( item.getTitle().equals("Edit")){
            callEdit();
        } else if( item.getTitle().equals("Export")) {
            callShare();
        } else if( item.getTitle().equals("Delete")) {
            callDelete();
        }
        return true;
    }
});

popup.show();

Styles.xml

<style name="AppBaseTheme" parent="@android:style/Theme.Light.NoTitleBar">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:popupMenuStyle">@style/PopupMenu</item>      
</style>

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
    <item name="android:textColor">#FF01F0</item>
    <item name="android:textSize">12sp</item>
</style>

但它没有改变文字颜色。

1 个答案:

答案 0 :(得分:13)

您可以通过将此代码添加到styles.xml来更改文本大小和颜色,并在清单文件中使用它。对我而言,它有效。

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
    <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
</style>

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
    <item name="android:textColor">#FF01F0</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="myPopupMenuTextAppearanceSmall" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
    <item name="android:textColor">#545656</item>
    <item name="android:textSize">15sp</item>
</style>

<style name="myPopupMenuTextAppearanceLarge" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large">
    <item name="android:textColor">#545656</item>
    <item name="android:textSize">25sp</item>    
</style>