问题
I want to change the text size of popup menu item. I have written this code to create a popup menu. But my problem is I couldnot access the textView of popupmenu ie"EXIT" so that i could use
exitItem.setTextSize(40);
//my code
popupmenu = new PopupMenu(MainActivity.this, findViewById(R.id.actionbar_item));
popupmenu.getMenu().add(Menu.NONE, 0, Menu.NONE, "Exit");
popupmenu.setOnMenuItemClickListener(this);
I have not created any seperate xml file to inflate these items. I want to do it programatically as these popupitems are dynamically generated.
Thanks in advance.
回答1:
Unfortunately, it's not trivial. To change MenuItem appearance, even in this simple case, you'll have to set a custom View to it. But that will work only if MenuItem is shown as action, i.e. not in a list, which is not your case. To customise the appearance of your list in PopupMenu you have to use PopUpWindow instead. See this answer for example.
In your case, as you want to add elements dynamically maybe the best solution will be to implement your own Adapter with proper getView() method and use it together with ListView and PopUpWindow.
回答2:
I'm afraid it cannot be done like that. The closest thing you can do is the following:
- Create a
ViewpreviousViewwhich implementsR.id.actionbar_item - Use that
Viewto manipulate the text size - Add an item to your
PopupMenuviapopupmenu.getMenu().add(Menu.NONE, 0 , Menu.NONE, "Exit"); - Create a
MenuItemobjectmenuItemviaMenuItem menuItem = popupmenu.getMenu().getItem(0); - Then set
menuItem.setActionView(previousView);
This is your best bet, but I haven't tested it yet. Good luck!
来源:https://stackoverflow.com/questions/20451357/changing-text-size-of-popup-menu-item