简体   繁体   English

如何仅在主要活动中显示菜单?

[英]How to only show the menu in the main activity?

I want to only show my menu in my main activity then have a back button from my other activities instead of a menu. 我只想在主活动中显示菜单,然后在其他活动中有一个后退按钮,而不是菜单。 Right now I am just wondering how would I would remove the menu from the action bar on the activities I don't want it on. 现在,我只是想知道如何从不需要的活动的操作栏中删除菜单。

My Manifest: 我的清单:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Insulter"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="Favourites"
        android:launchMode = "singleInstance">
    </activity>

    <activity
        android:name="Settings"
        android:launchMode = "singleInstance">
    </activity>


</application>

My menu opener inside my main activity: 我主要活动中的菜单打开器:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent startSettings = (new Intent(Insulter.this,Settings.class));
        startActivity(startSettings);
        return true;
    } else if (id == R.id.exit_the_app) {
        finish();
        return true;
    } else if (id == R.id.favourites) {
        Intent startFavs = (new Intent(Insulter.this, Favourites.class));
        String[] objects = new String[favs.size()];
        favs.toArray(objects);
        final ArrayList<String> list = new ArrayList<>(Arrays.asList(objects));
        startFavs.putStringArrayListExtra("favs",list);
        startActivity(startFavs);
        return true;
    }


    return super.onOptionsItemSelected(item);
}

Activity.onCreateOptionsMenu is the place where it gets created. Activity.onCreateOptionsMenu是创建它的地方。 Just don't override this method or let it return false to not show the menu. 只是不要覆盖此方法,或者让它返回false即可不显示菜单。

You must return true for the menu to be displayed; 您必须返回true才能显示菜单。 if you return false it will not be shown. 如果返回false,则不会显示。

If you're using the same implementation for all your activities, define a field boolean isMain and return it from onCreateOptionsMenu 如果您对所有活动使用相同的实现,请定义一个boolean isMain字段,并从onCreateOptionsMenu返回

public boolean onCreateOptionsMenu (Menu menu) {
    if (!isMain)
        return false;

    [creating the menu here like before]
}

如果您想在任何活动中使用菜单,则需要覆盖onCreateOptionsMenuonOptionsItemSelected ...。如果您不想使用菜单,则只需在活动中不覆盖这些方法...

For each activity that you create, there must be a menu file associated with it in the res/menu folder. 对于您创建的每个活动,在res/menu文件夹中必须有一个与之关联的res/menu文件。 Simply delete or comment the corresponding <item> tags. 只需删除或注释相应的<item>标签。 You may experiment with them by looking at the output show in the preview screen. 您可以通过查看预览屏幕中的输出来对它们进行试验。

Note: I think this works only when an activity is created automatically using the UI (ie not manually) since the corresponding menu file is generated automatically with it. 注意:我认为这仅在使用UI自动创建活动(即不手动)时有效,因为相应的菜单文件是使用该UI自动生成的。

You need to use following steps to remove options menu and add back button in your non-main activities -- 您需要按照以下步骤删除非主要活动中的选项菜单添加返回按钮 -

  1. Don't override onCreateOptionsMenu method. 不要覆盖onCreateOptionsMenu方法。
  2. call getActionBar().setDisplayHomeAsUpEnabled(true) in your activity's onCreate method. 在您活动的onCreate方法中调用getActionBar()。setDisplayHomeAsUpEnabled(true)
  3. Override onOptionsItemSelected method in following way-- 以下列方式覆盖onOptionsItemSelected方法-

     @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case android.R.id.home: finish(); return super.onOptionsItemSelected(item); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅显示一次登录活动,并在下次启动主活动时显示 - Show login activity only once and on next start main activity Android-重新创建主要活动后菜单项未显示 - Android - Menu Item does not show up after recreating Main Activity Android Studio,选项菜单仅在主活动中打开 - Android Studio, options menu only opens in Main Activity 如何在主要活动的TextView上显示状态消息? - How to show status message on TextView in Main Activity? 如何在每个活动中显示“滑动菜单栏”? - How to show Sliding Menu Bar in every activity? 如何在Android中使用菜单从Main活动中打开Intent(MapView) - How to open an Intent (MapView) from the Main activity with menu in Android 如何在恢复主要活动之前显示 admob 插页式广告 - How to show admob interstitial before resuming main activity 聚焦分离视图时如何在 Mac OS 上显示主菜单? - How to show main menu on Mac OS when a detached view is focused? 我的操作栏菜单不在其他活动中显示,而仅在主活动中显示(我正在使用Fragment和Non Fragment) - My Action bar menu is not display in another activity but it display in the main Activity only(i'm using Fragment and non Fragment) 菜单不会显示在“活动”中 - Menu won't show in Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM