问题
I am using MenuBar from GWT showcase. I want to add text in the menubar. Can you please let me know that in which way I can get it ?
回答1:
You can try this one also. Its given in MenuBar Docs API
public MenuItem addItem(java.lang.String text,
boolean asHTML,
MenuBar popup)
So, In your case,
final String image = "<img src='"+GWT.getModuleBaseURL() + "/images/down-arrow.png' height='25px' width='25px'/>";
HomeMenu.addItem(new MenuItem(image,true,mainMenu));
回答2:
The best approach is not to hack anything. Just use the Image class:
MenuItem profileMenuItem = new MenuItem(new Image(TripImages.INSTANCE.getUserSmall()) + " Profile", true, profileMenuDropdown);
Works for me.
回答3:
I have found the solution. Maybe it will help somebody...
MenuBar HomeMenu = new MenuBar();
final String image = "<img src='"+GWT.getModuleBaseURL() + "/images/down-arrow.png' height='25px' width='25px'/>";
SafeHtml addActivityImagePath = new SafeHtml() {
@Override
public String asString() {
return image;
}
};
HomeMenu.addItem(new MenuItem(addActivityImagePath,mainMenu));
回答4:
If you're using bundle
and ImageResource
, you can use AbstractImagePrototype
.
If GlobalBundle.INSTANCE.home()
is my ImageResource
, this makes it appear on the menu:
menu.addItem(AbstractImagePrototype.create(GlobalBundle.INSTANCE.home()).getSafeHtml(), ...);
回答5:
Here a link to a simple example of creating menubar. And here is an example of creating a menu with submenus.
回答6:
MenuBar HomeMenu = new MenuBar();
final String image = "<img src='"+GWT.getModuleBaseURL() + "/images/down-arrow.png' height='25px' width='25px'/>";
SafeHtml addActivityImagePath = new SafeHtml() {
@Override
public String asString() {
return image;
}
};
HomeMenu.addItem(new MenuItem(addActivityImagePath,mainMenu));
来源:https://stackoverflow.com/questions/9903738/image-in-gwt-menubar-instead-of-text