Java 向空活动添加菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36090542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:26:24  来源:igfitidea点击:

Add a menu to an empty activity

javaandroid-menu

提问by Craig Gallagher

I have made an android application in Android Studio and would like to create a option menu on it. I created it as an empty activity and now realize I would have been better creating a blank activity to get the option menu. Is there anyway to create the option menu in a empty activity. If someone could point me to a tutorial that would be great this is my code so far for my menu.

我在 Android Studio 中制作了一个 android 应用程序,并想在其上创建一个选项菜单。我将它创建为一个空活动,现在意识到我最好创建一个空白活动来获取选项菜单。无论如何要在空活动中创建选项菜单。如果有人能给我指出一个很棒的教程,那么这是我迄今为止的菜单代码。

menu_menu

menu_menu

<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="saveourcar.soc.MainActivity">
    <item
        android:id="@+id/action_Menu"
        android:orderInCategory="100"
        android:title="Menu"
        app:showAsAction="never" >
    <menu>
        <item
            android:id="@+id/instructions"
            android:title="Instructions"
            android:icon="@drawable/bg"/>
        <item
            android:id="@+id/hotels"
            android:title="Hotels"
            android:icon="@drawable/mechanic"/>

    </menu>
    </item>
</menu>

Main activity

主要活动

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_Menu) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

采纳答案by Zaid Qureshi

You need to inflate your menu. These tutorialsshow how to use menus. So something like this, and choose a better name than menu_menu:

你需要增加你的菜单。这些教程展示了如何使用菜单。所以像这样,并选择一个比 menu_menu 更好的名称:

public boolean onCreateOptionsMenu(Menu menu) {
 MenuInflater inflater = getMenuInflater();
 inflater.inflate(R.menu.menu_menu, menu);
 return true;
}

回答by mazhar

when you make a menu's layout you need to define it for the Activity you want to place it in. You may do so by:

当您制作菜单布局时,您需要为要放置它的活动定义它。您可以这样做:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater findMenuItems = getMenuInflater();
        findMenuItems.inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

the main_menuis your menu's layout name, and findMenuItemsis an optional name.

themain_menu是菜单的布局名称,findMenuItems是一个可选名称。

And to make your menu's items clickable for an About menu and exiting the app you'll need this:

并且要使您的菜单项可单击以获取关于菜单并退出应用程序,您将需要以下内容:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.aboutMenuItem:
                Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(aboutIntent);
                break;
            case R.id.exitMenuItem:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

回答by Md Hasan Mia

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId()==R.id.action_Menu){
            Toast.makeText(this, "Action Item", Toast.LENGTH_SHORT).show();
        }

        if (item.getItemId()==R.id.instructions){
            Toast.makeText(this, "Hnstructions Item", Toast.LENGTH_SHORT).show();
        }

        if (item.getItemId()==R.id.hotels){
            Toast.makeText(this, "Hotels Item", Toast.LENGTH_SHORT).show();
        }

        return super.onOptionsItemSelected(item);
    }
}

回答by pratham kesarkar

With the new Material Design Principal you have to setSupportActionBar(toolbar)inside your activity inorder to show menu.

使用新的 Material Design Principal,您必须setSupportActionBar(toolbar)在 Activity 中才能显示菜单。

class HomeActivity:BaseActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_home)
       setSupportActionBar(toolbar)
   }

  override fun onCreateOptionsMenu(menu: Menu?): Boolean {
      menuInflater.inflate(R.menu.menu_new,menu)
      return true
   }
}