如何隐藏或添加风味菜单项? [英] How to hide or add menu items for Flavor?

查看:109
本文介绍了如何隐藏或添加风味菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有3种不同口味的应用程序,分别是fullpart1part2.

I have an app that has 3 different flavors, full, part1 and part2.

所有不同口味的包装名称都不同,因此我可以将它们作为不同的应用程序提供.

All different flavors have different package names, so I can ship them as different apps.

现在,我希望仅part1获得一个名为Reload的菜单项.其他2种口味不应包含此菜单项.这可能吗?

Now I want that only part1 gets a menu item called Reload. The other 2 flavors shouldn't have this menu item. Is this possible?

我尝试了以下菜单资源:

I tried the following with the menu resources:

app
|
+-src
  |
  +-full
  |
  +-main
  | |
  | +-res
  |   |
  |   +-menu
  |     |
  |     +-main_activity.xml
  |
  +-part1
  | |
  | +-res
  |   |
  |   +-menu
  |     |
  |     +-main_activity.xml
  |
  +-part2

其中part1main_activity.xml是:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_reload"
        android:icon="@drawable/ic_reload"
        android:title="@string/action_reload"
        app:showAsAction="always"/>
</menu>

mainmain_activity.xml是:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
</menu>

但是,如果我使用part1以外的任何其他构建变体来构建应用程序,则在MainActivity中会出现编译错误,我需要对菜单选择做出反应:

However, if I build the app in any other build variant than part1, I get a compilation error in my MainActivity where I need to react to the menu selection:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_reload: // Compile error: This item is not available
            // TODO reload
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

这是很明显的.但是,您有什么建议来解决针对不同构建风格的菜单的解决方案吗?

It's quite obvious why that is. But do you have any suggestion what the solution is to customize menus for different build flavors?

推荐答案

在处理常规普通代码的主源文件夹中创建一个MainActivity.在part1源文件夹中创建另一个MainActivity,您可以在其中覆盖onOptionsItemSelected的地方,对R.id.action_reload的引用不是问题.那应该起作用.

Create a MainActivity in the main source folder where you handle the normal common code. Create another MainActivity in the part1 source folder where you override onOptionsItemSelected where it's not a problem to have references to R.id.action_reload. That should work.

这篇关于如何隐藏或添加风味菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆