changing text size of popup menu item

大兔子大兔子 提交于 2019-12-07 16:45:48

问题


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 View previousView which implements R.id.actionbar_item
  • Use that View to manipulate the text size
  • Add an item to your PopupMenu via popupmenu.getMenu().add(Menu.NONE, 0 , Menu.NONE, "Exit");
  • Create a MenuItem object menuItem via MenuItem 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!