Android 检查MenuItem是否处于ActionBar溢出状态
问题:如果某个菜单项(或哪个菜单项)在ActionBar的溢出菜单中,是否有方法签入代码? 我用的是ActionBarSherlock 我之所以需要它,是因为我有一堆图标,如果有空间的话,它们会显示在ActionBar中。我有一个全息黑暗的主题,所以图标是适合的Android 检查MenuItem是否处于ActionBar溢出状态,android,overflow,actionbarsherlock,menuitem,Android,Overflow,Actionbarsherlock,Menuitem,问题:如果某个菜单项(或哪个菜单项)在ActionBar的溢出菜单中,是否有方法签入代码? 我用的是ActionBarSherlock 我之所以需要它,是因为我有一堆图标,如果有空间的话,它们会显示在ActionBar中。我有一个全息黑暗的主题,所以图标是适合的 当菜单项被放入溢出菜单时,我的问题来了。在预蜂窝设备上,这意味着当用户按下菜单按钮时,它们将显示。此菜单与我的操作栏背景完全相反,我希望有一组不同的图标来适应它。我可能已经找到了解决此问题的方法:在设计指南()中,有一个表格显示了根据d
当菜单项被放入溢出菜单时,我的问题来了。在预蜂窝设备上,这意味着当用户按下菜单按钮时,它们将显示。此菜单与我的操作栏背景完全相反,我希望有一组不同的图标来适应它。我可能已经找到了解决此问题的方法:在设计指南()中,有一个表格显示了根据dip中的宽度显示的操作栏项数 基于该表,我编写了以下代码:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem search = menu.findItem(R.id.menu_search);
// Get width in dp
DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
float logicalDensity = metrics.density;
int dp = (int) (display.getWidth() / logicalDensity + 0.5);
if (dp < 360) { // only two icons
search.setIcon(R.drawable.ic_menu_search); // Show menu icon for pre-3.0 menu
} else {
search.setIcon(R.drawable.ic_action_search); // Show action bar icon for action bar
}
return true;
}
@覆盖
公共布尔值OnPrepareOptions菜单(菜单){
MenuItem search=menu.findItem(R.id.menu\u search);
//获取dp中的宽度
DisplayMetrics=新的DisplayMetrics();
Display Display=getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
浮点逻辑密度=metrics.density;
int dp=(int)(display.getWidth()/logicalDensity+0.5);
如果(dp<360){//只有两个图标
search.setIcon(R.drawable.ic_menu_search);//显示3.0之前菜单的菜单图标
}否则{
search.setIcon(R.drawable.ic_action_search);//显示操作栏的操作栏图标
}
返回true;
}
我发布了一个类似问题的答案,可以解决您的问题,请参阅:
基本上,您可以使用OnPrepareOptions功能表删除非操作项图标。您可以使用反射。将以下代码放入一个类中,然后调用
Foo.isInOverflow(yourMenuItem)代码>
图标不会显示在溢出菜单项中。我知道它们不会。这不是我要问的。我指出我对pre-honeycomb设备(菜单图标确实显示)感兴趣。如果我们找到一种方法,可以禁用溢出菜单中菜单项上的图标,以便只在传统菜单中显示文本,这也会很有帮助。你应该看看,这会更容易…我已经看过很多次这个网站了。你能具体说明我应该在哪里寻找吗?将此更改为与android.support.v7.internal.view.menu.menuitempl
一起使用,但随后我在API 10上得到了一个NoSuchMethodException
(即使我正在使用具有该方法的appcompat版本22构建)。当在API 22上运行时,它可以工作,但给出了错误的结果(所有操作/工具栏图标都被称为处于溢出状态,尽管其中只有一些是…。@Matthias,我更新了代码。如果您仍然有问题或发现问题,请告诉我。
protected static final String SUPPORTCLASS = "android.support.v7.internal.view.menu.MenuItemImpl";
protected static final String NATIVECLASS = "com.android.internal.view.menu.MenuItemImpl";
protected static Method sSupportIsActionButton;
protected static Method sNativeIsActionButton;
static {
try {
Class<?> MenuItemImpl = Class.forName(NATIVECLASS);
sNativeIsActionButton = MenuItemImpl.getDeclaredMethod("isActionButton");
sNativeIsActionButton.setAccessible(true);
} catch (Exception ignored) {
}
try {
Class<?> MenuItemImpl = Class.forName(SUPPORTCLASS);
sSupportIsActionButton = MenuItemImpl.getDeclaredMethod("isActionButton");
sSupportIsActionButton.setAccessible(true);
} catch (Exception ignored) {
}
}
// --------------------------------------------------------------------------------------------
/**
* Check if an item is showing (not in the overflow menu).
*
* @param item
* the MenuItem.
* @return {@code true} if the MenuItem is visible on the ActionBar.
*/
public static boolean isActionButton(MenuItem item) {
switch (item.getClass().getName()) {
case SUPPORTCLASS:
try {
return (boolean) sSupportIsActionButton.invoke(item, (Object[]) null);
} catch (Exception e) {
// fall through
}
case NATIVECLASS:
try {
return (boolean) sNativeIsActionButton.invoke(item, (Object[]) null);
} catch (Exception e) {
// fall through
}
default:
return true;
}
}
/**
* Check if an item is in the overflow menu.
*
* @param item
* the MenuItem
* @return {@code true} if the MenuItem is in the overflow menu.
* @see #isActionButton(MenuItem)
*/
public static boolean isInOverflow(MenuItem item) {
return !isActionButton(item);
}
-keep public class android.support.v7.internal.view.menu.** { *; }