开发者

Writing text on icon of Menu in Android?

开发者 https://www.devze.com 2022-12-26 14:57 出处:网络
Can we set Title for a Menu开发者_开发知识库 Item in Android if it has an icon from drawable?

Can we set Title for a Menu开发者_开发知识库 Item in Android if it has an icon from drawable?

I have a icon for a Menu Item and if I set the icon then the title that is set for that Menu item is not visible.

Is this possible or not?

Hope to get a reply soon.

Regards Sunil


It's definitely possible. Code like this works:

public boolean onCreateOptionsMenu(Menu menu){
    menu.add(0, MENU_PAUSE, 0, "Pause").setIcon(R.drawable.pause);
    return true;
}

That creates a menu with one item in it, referred to by the int MENU_PAUSE (a unique identifier number), with the title "Pause" and the icon pause from res/drawable.

If that doesn't do it for you, could you show us your code?


Edit to provide more info based on comments:

It turns out the size of the images matters. The default Android menu images are 48*48 or smaller so it would seem that one must use similar sizes as well. Note: I didn't experiment with different screen sizes, this was only with 320x480 screen.

The alternative is to change the default background of the menu. These can be modified by setting a theme for your application in the manifest and then putting in these lines:

<item name="android:panelBackground">@drawable/yourMenuDrawable</item>
<item name="android:panelFullBackground">@drawable/yourOtherMenuDrawable</item>

The normal menu uses panelFullBackground, so that should be the one you replace first. You can find the default images Android uses in yourSDKfolder/platforms/xxx/data/res/, and they're called menu_background.9.png and menu_background_fill_parent_width.9.png.

I found this information by looking at the default styles and themes files available from the Android source code website


Yes, you can. Just add withText to the android:showAsAction attribute. Example:

<item android:id="@+id/menu_save"
      android:icon="@drawable/ic_menu_save"
      android:title="@string/menu_save"
      android:showAsAction="ifRoom|withText" />

See https://developer.android.com/guide/topics/ui/actionbar.html for more details :)

0

精彩评论

暂无评论...
验证码 换一张
取 消