使用AppCompat主题

时间:2015-06-25 07:18:06

标签: android menu menuitem

我试图在菜单操作附近显示图标,但出于某种原因,当我使用AppCompatActivity / AppCompat主题时,我无法显示它们。

我只收到文字。

我尝试了以下内容:

   @Override
        public boolean onMenuOpened(int featureId, Menu menu)
        {
            if(featureId == Window.FEATURE_ACTION_BAR && menu != null)
            {
                if(menu.getClass().getSimpleName().equals("MenuBuilder"))
                {
                    try
                    {
                        Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                        m.setAccessible(true);
                        m.invoke(menu, true);
                    }
                    catch(NoSuchMethodException e)
                    {
                        Log.e("Menu:", "onMenuOpened", e);
                    }
                    catch(Exception e)
                    {
                        throw new RuntimeException(e);
                    }
                }
            }
            return super.onMenuOpened(featureId, menu);
        }

仍然相同:(

以下是菜单ITEMS(menu.xml):

menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.ylx.tools.MainActivity">

    <item
        android:id="@+id/action_login"
        android:icon="@drawable/ic_action_person"
        android:orderInCategory="100"
        android:title="@string/action_login"/>
    <item
        android:id="@+id/action_wishlist"
        android:icon="@drawable/ic_action_favorite"
        android:orderInCategory="100"
        android:title="@string/action_wish"/>
    <item
        android:id="@+id/action_track_order"
        android:icon="@drawable/ic_action_place"
        android:orderInCategory="100"
        android:title="@string/action_track"/>
    <item
        android:id="@+id/action_rate_the_app"
        android:icon="@drawable/ic_action_important"
        android:orderInCategory="100"
        android:title="@string/action_rate"/>
    <item
        android:id="@+id/action_share_app"
        android:icon="@drawable/ic_action_share"
        android:orderInCategory="100"
        android:title="@string/action_share"/>
    <item
        android:id="@+id/action_contact_us"
        android:icon="@drawable/ic_action_phone"
        android:orderInCategory="100"
        android:title="@string/action_contact"/>
    <item
        android:id="@+id/action_policies"
        android:icon="@drawable/ic_action_about"
        android:orderInCategory="100"
        android:title="@string/action_policies"/>

    <item
        android:id="@+id/action_cart"
        android:title="@string/action_cart"
        android:actionLayout="@layout/cart"
        android:orderInCategory="9999999"
        android:icon="@drawable/empty_cart_final"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_fav"
        android:title="@string/action_fav"
        android:actionLayout="@layout/wishlist_list"
        android:icon="@drawable/fav_final"
        app:showAsAction="always" />
</menu>

工具栏仅在Navigation Drawer.xml中创建

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/navigationHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/parentContentHost"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@+id/contentHost"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <ExpandableListView
            android:id="@+id/mainList"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            android:groupIndicator="@null" />
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

toolbar.xml包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

这是我正在使用的Style主题:

<resources>

    <style name="Theme.ylg" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimaryDark">#004D40</item>
    <item name="colorPrimary">#00695C</item>
    <item name="android:textColorPrimary">#fdfff2</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:editTextColor">@android:color/background_dark</item>
    <item name="android:textColor">@android:color/background_dark</item>
    <item name="colorControlNormal">@android:color/background_dark</item>
    <item name="colorControlActivated">#ff0043c5</item>
    <item name="colorControlHighlight">#00695C</item>
    </style>

    <style name="Theme.ylg.NoTitle" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
    </style>
</resources>

这里有什么问题?如何显示图标?

2 个答案:

答案 0 :(得分:7)

对于AppCompatActivity,您可以将此检查放在onPrepareOptionsPanel()上。

@Override 
    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
            if (menu != null) {
                if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
                    try { 
                        Method m = menu.getClass().getDeclaredMethod(
                                "setOptionalIconsVisible", Boolean.TYPE);
                        m.setAccessible(true);
                        m.invoke(menu, true);
                    } catch (Exception e) {
                        Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
                    } 
                } 
            } 
        return super.onPrepareOptionsPanel(view, menu);
    } 

答案 1 :(得分:0)

谢谢! 另外,将代码放在onPrepareOptionsMenu(菜单菜单)中即可, 像这样

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
        if (menu != null) {
          if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
              try { 
                  Method m = menu.getClass().getDeclaredMethod(
                          "setOptionalIconsVisible", Boolean.TYPE);
                  m.setAccessible(true);
                  m.invoke(menu, true);
              } catch (Exception e) {
                  Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
              } 
          } 
      } 
        return super.onPrepareOptionsMenu(menu);
    }