@斯坦的解决方案并没有为我工作,所以这里是我的方式来实现对动作条的顶子菜单(但低于当然的主菜单):
我已创建2个xml文件:menu_main。XML和menu_more.xml位于在res /菜单目录
第一个 'menu_main.xml' 包含菜单:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- our addMenu doesn't have sub-items-->
<item
android:id="@+id/action_add"
android:icon="@drawable/ic_note_add_white_24dp"
android:title="@string/action_add"
app:showAsAction="ifRoom"/>
<!-- our moreMenu which show drop-down menu when clicked-->
<item
android:id="@+id/action_more"
android:icon="@drawable/ic_more_vert_white_24dp"
android:title="@string/action_more" <!--in text: "more"-->
app:showAsAction="always"/>
</menu>
,第二个“menu_more.xml '包含下拉菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- This menu will be hidden by default-->
<!-- But will be visible when moreMenu with '@+id/action_more' is clicked-->
<item
android:id="@+id/action_settings"
app:showAsAction="ifRoom|withText"
android:title="@string/action_settings" <!-- In text: "Settings"-->
android:visible="true"/>
</menu>
这是以前的我新加坡国立大学的样子:
result-after-add-2-xmls(我没有足够的信誉10来显示图像)
在活动中,我重写了这个方法:
公共布尔onPrepareOptionsMenu(菜单菜单)
在前面的方法中,我得到了对主菜单项的引用(在这种情况下是菜单@ + id/action_more位于m enu_main.xml文件),然后设置setOnMenuItemClickListener就可以了,最后,声明并成立了的PopupMenu实例来管理和显示子菜单项:
// show popup menu when menuMore clicked
menu.findItem(R.id.action_more).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
{
// get reference to menuMore item
View menuMore = findViewById(item.getItemId());
// create a popup anchored to the view (menuMore)
// notes: if declare and set up PopupMenu Outside of this onMenuItemClick()
// then it'll not work!
// Because: the view you put into PopupMenu() could be null
final PopupMenu popupMenu = new PopupMenu(getApplicationContext(), menuMore);
// inflate 'menu_more.xml' layout file
// which contain all sub-items of menu
popupMenu.getMenuInflater().inflate(R.menu.menu_more, popupMenu.getMenu());
// process popup clicks on sub-items
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "showing SettingsActivity..",
Toast.LENGTH_SHORT).show();
break;
// more items go here
}
return true;
}
});
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
这里是最终结果:
final-look-drop-down-menu
想通了吗? – JackyBoi 2015-04-17 13:44:46
同样的问题在这里(Android 5.1)。在Android开发过程中,经过几年来这些“特性”的磕磕绊绊,我必须说Android是令人沮丧的平台,唉。 – Stan 2015-05-26 12:13:00