简体   繁体   English

如何在Android应用中添加操作栏?

[英]How to add action bar to Android app?

I just start with Android. 我只是从Android开始。 Want to add action bar following the tutorial . 要在本教程之后添加操作栏。 I was added res/menu/main_activity_actions.xml : 我被添加到res/menu/main_activity_actions.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      NecoSeparator:showAsAction="ifRoom"  />

<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="never" />

In MainActivity.java i do: MainActivity.java我这样做:

  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

But Eclipse says that cant find : R.menu.main_activity_actions . 但是Eclipse说找不到: R.menu.main_activity_actions

What's wrong with this code? 此代码有什么问题? I have to do something more with this xml file? 我必须对这个xml文件做更多的事情吗?

UPDATE 更新

MainActivity.java

package com.example.necoseparator;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                openSearch();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

    }

I Think main problem is to saving your menu layout, if you have an error in file its might happened. 我认为主要问题是保存菜单布局,如果文件中有错误可能会发生。 please change your menu file with my code and save that and import R file (make sure that is not android.R and its must be YourPackageName.R ) then clean your project and run. 请使用我的代码更改菜单文件,然后保存并导入R文件(确保该文件不是android.R并且必须为YourPackageName.R ),然后清理项目并运行。

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="filtered"
    />

<item
    android:id="@+id/menu_load"
    android:icon="@drawable/ic_luncher"
    android:orderInCategory="200"
    android:showAsAction="always"
    android:title="update"/>

 </menu>

Remove this line xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto and try again 删除此行xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto然后重试

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>

You can use Toolbar 您可以使用工具栏

toolbar = (Toolbar) findViewById(R.id.toolbar1); 工具栏=(工具栏)findViewById(R.id.toolbar1); setSupportActionBar(toolbar); setSupportActionBar(toolbar);

and add list of menus 并添加菜单列表

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM