开发者

dynamically build menu filename

开发者 https://www.devze.com 2023-01-26 08:18 出处:网络
I have multiple menu files menu_language_en, menu_language_es...etc I would like to load them using another variable, something like:

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().

0

精彩评论

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

关注公众号