簡體   English   中英

Android:自定義應用程序的菜單(例如背景顏色)

[英]Android: customize application's menu (e.g background color)

有什么方法(如果有辦法)自定義菜單(由手機的MENU按鈕觸發的菜單)。 我對兩件事特別感興趣:

  • 將背景顏色從標准淺灰色更改為深灰色
  • 菜單項如何對齊。 我有4個項目,它們自動對齊2x2,但我更喜歡將它們全部放在一行(1x4)

我創建了自己的菜單類。 它可能不是你想要的,但它應該有希望讓你開始。 這是我發布的文章和源代碼的可下載鏈接。

http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx

沒有內置的菜單框架。

歡迎您截取MENU按鈕(通過onKeyDown()或其他東西)並渲染您想要的內容,但請記住,用戶希望它看起來像其他菜單在他們的設備上。

您還可以實現“onCreateOptionsMenu”方法,該方法通常用於顯示標准菜單,並在這種情況下顯示您想要的任何內容。

在我的游戲中,當按下菜單按鈕時,我實現了它以顯示“Game Paused”對話框...

使用樣式。 這適用於Android 5.0

<style name="AppTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:actionOverflowMenuStyle">@style/PopupMenu.MyStyle</item>
</style>

<style name="PopupMenu.MyStyle" parent="android:Widget.PopupMenu">
    <item name="android:popupBackground">@drawable/actionbar_item_background</item>
</style>

...然后drawable是一個常規選擇器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/primary"/>
    <item android:drawable="@color/highlighted" android:state_pressed="true"/>
</selector>

主題中style.xml中的背景菜單顏色

<item name="android:panelFullBackground">@android:color/darker_gray</item>

這個答案有效但在使用ActionBarSherlock時崩潰了。 盡管如此,這仍然是一個hacky解決方法。

    // Black Vodoo! Do not try this at home.

    final LayoutInflater li = getLayoutInflater();

    final Class<LayoutInflater> clazz = LayoutInflater.class;

    try {
        final Field fieldSet = clazz.getDeclaredField("mFactorySet");
        fieldSet.setAccessible(true);
        fieldSet.setBoolean(li, false);

        li.setFactory(new Factory() {

            @Override
            public View onCreateView(final String name,
                    final Context context, final AttributeSet attrs) {
                if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                    try {
                        final LayoutInflater f = getLayoutInflater();
                        final View view = f.createView(name, null, attrs);
                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                // Set the text color
                                ((TextView) view).setTextColor(Color.WHITE);
                            }
                        });
                        return view;
                    } catch (final Exception e) {
                    }
                }
                return null;
            }
        });
    } catch (final Exception e) {
        e.printStackTrace();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM