应用程序启动时没有操作栏 [英] Application doesn't have Action bar when starts

查看:90
本文介绍了应用程序启动时没有操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android studio中为我的页面制作一个操作栏菜单。但是当我运行应用程序时没有操作栏。

i在 menu_main.xml 中编写菜单XML代码,如下所示:

i want to make an Action bar Menu for my page in android studio. but when i run the app there is no Action bar.
i write the menu XML codes in menu_main.xml like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item

    android:id="@+id/SigningUp"

            showAsAction="ifRoom|withText"

         android:title="ثبت نام"/>
   </menu>





这是我的 java 代码:



and this is my java code:

    public static final int SIGN_UP_ID = Menu.FIRST;
    public static final int EXIT_APP_ID = Menu.FIRST + 1;

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        menu.add(0, SIGN_UP_ID, 0, R.string.sign_up);
        menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);


        return result;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch(item.getItemId())
        {
            case SIGN_UP_ID:
                Intent i = new Intent(LoggingIn.this, signingUp.class);
                startActivity(i);
                return true;
            case EXIT_APP_ID:

                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

推荐答案

根据文档 [ ^ ],

According to the documentation[^],
Quote:

onCreateOptionsMenu(菜单菜单)初始化Activity的标准选项菜单的内容。您应该将菜单项放入菜单中。仅在第一次显示选项菜单时调用此选项。要在每次显示时更新菜单,请参阅onPrepareOptionsMenu(菜单)。

onCreateOptionsMenu (Menu menu) initializes the contents of the Activity's standard options menu. You should place your menu items in to menu. This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).



onPrepareOptionsMenu(菜单菜单) [ ^ ]

因此,在onPrepareOptionsMenu(菜单)中添加菜单项代码。


onPrepareOptionsMenu (Menu menu)[^]
So, place add menu item code inside onPrepareOptionsMenu(Menu).


这篇关于应用程序启动时没有操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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