dynamically build menu filename
I have multiple menu files menu_language_en, menu_language_es...etc I would like to load them using another variable, something like:
String lang="en";
inflater.in开发者_StackOverflow社区flate(eval("R.menu.language_menu_"+lang), menu);
Any idea??
Thanks jo
The right answer is to use resource sets, so you have a single filename in different directories based on language (e.g., res/menu/options.xml
, res/menu-de/options.xml
, res/menu-zh/options.xml
, etc.). You, in your code, just inflate(R.menu.options, menu)
, and Android chooses the right language based on the user's locale.
If for some reason that solution is unsuitable, use getResources().getIdentifier()
to derive the resource ID given the name, then use that value with your call to inflate()
.
精彩评论