Android操作栏按钮未显示

时间:2015-04-22 11:23:33

标签: android android-studio android-actionbar

我正在为我的应用实施一个操作栏,其中包含2个菜单按钮:添加和设置。在Designer视图中,2个按钮仍然像普通一样。但是,当我运行应用程序时,2按钮根本不显示。请帮忙

以下是我的代码:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

MainActivity.Java

package com.example.sunny.mynote;

import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteDataSource;
import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteItem;

import java.util.List;


public class MainActivity extends ListActivity {

    public static final int EDITOR_ACRIVITY_REQUEST = 1001;
    public static final int SETTINGS_REQUEST = 1003;
    private static final int MENU_DELETE_ID = 1002;
    private int currentNoteId;
    private NoteDataSource datasource;
    List<NoteItem> notesList;


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

        datasource = new NoteDataSource(this);

        refreshDisplay();
    }

    private void refreshDisplay() {
        notesList = datasource.findAll();
        ArrayAdapter<NoteItem> adapter =
                new ArrayAdapter<NoteItem>(this, R.layout.list_item_layout, notesList);
        setListAdapter(adapter);
    }


    //Add the Actions to the Action Bar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    //Respond to Action Buttons
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_create:
                createNote();
                return true;
            case R.id.action_settings:
                Settings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void createNote()
    {
        NoteItem note = NoteItem.getNew();
        Intent intent = new Intent(this, NoteEditorActivity.class);
        intent.putExtra("key", note.getKey());
        intent.putExtra("text", note.getText());
        startActivityForResult(intent, EDITOR_ACRIVITY_REQUEST);
    }

    private void Settings()
    {
        Intent intent2 = new Intent(this, Settings.class);
        startActivityForResult(intent2, SETTINGS_REQUEST);
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        NoteItem note = notesList.get(position);
        Intent intent = new Intent(this, NoteEditorActivity.class);
        intent.putExtra("key", note.getKey());
        intent.putExtra("text", note.getText());
        startActivityForResult(intent, EDITOR_ACRIVITY_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == EDITOR_ACRIVITY_REQUEST && resultCode == RESULT_OK)
        {
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);
            NoteItem note = new NoteItem();
            note.setKey(data.getStringExtra("key"));
            note.setText(data.getStringExtra("text"));
            datasource.update(note);
            refreshDisplay();
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        currentNoteId = (int)info.id;
        menu.add(0, MENU_DELETE_ID, 0, "Delete");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        if (item.getItemId() == MENU_DELETE_ID)
        {
            NoteItem note = notesList.get(currentNoteId);
            datasource.remove(note);
            refreshDisplay();
        }

        return super.onContextItemSelected(item);
    }
}

menu.xml文件

    <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/action_create" android:title="@string/action_create"
        android:icon="@drawable/create_note"
        android:orderInCategory="100" app:showAsAction="ifRoom" />
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="ifRoom" android:icon="@drawable/ic_settings"/>
</menu>

3 个答案:

答案 0 :(得分:1)

在setContentView

之后在Java文件中使用
getSupportActionBar().show();

答案 1 :(得分:1)

我想看看这个Activity的菜单xml文件。 请尝试这个,更改图标和ID。 请注意, android:showAsAction =“always” NOT app:showAsAction =“always”

<item android:id="@+id/Add_New_Note"
    android:icon="@drawable/ic_action_new"
    android:title="Add new note"
    android:showAsAction="always"/>

我有同样的问题,希望它会有所帮助。

答案 2 :(得分:1)

在MainActivity中包含此代码

public class MainActivity extends ActionBarActivity {

在onCreate()

ActionBar actionBar = getSupportActionBar();

在res中提供添加和设置项 - &gt;菜单文件夹。

它将显示ActionBar。

在findViewById中提供扩展ListViewActivity