使用 AppCompat 主题时菜单图标不显示
Menu Icons not showing up when using AppCompat Theme
我试图在菜单操作附近显示图标,但由于某些原因,我在使用 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);
}
还是一样:(
这是菜单项 (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>
工具栏仅在导航中创建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" />
这是我使用的样式主题:
<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>
这里可能出了什么问题?如何显示图标?
对于 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);
}
谢谢!
另外,将代码放在onPrepareOptionsMenu(Menu menu)中就可以了,
像这样
@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);
}
我试图在菜单操作附近显示图标,但由于某些原因,我在使用 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);
}
还是一样:(
这是菜单项 (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>
工具栏仅在导航中创建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" />
这是我使用的样式主题:
<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>
这里可能出了什么问题?如何显示图标?
对于 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);
}
谢谢! 另外,将代码放在onPrepareOptionsMenu(Menu menu)中就可以了, 像这样
@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);
}