Android的动作栏按钮显示不出来按钮、动作、不出来、Android

2023-09-12 23:41:28 作者:女人不花,何来貌美如花

我实现了我的应用程序,它有2个菜单按钮的操作栏:添加和设置。在设计器中查看2个按钮仍然AP preAR像正常的。然而,当我运行的应用程序,2个按钮不显示在所有。请帮忙

下面是我的code:

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s机器人:layout_width =match_parent
    机器人:layout_height =match_parent工具:上下文=MainActivity。>

    <的ListView
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentBottom =真/>
< / RelativeLayout的>
 

MainActivity.Java

 包com.example.sunny.mynote;

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

进口com.example.sunny.mynote.com.example.sunny.mynote.data.NoteDataSource;
进口com.example.sunny.mynote.com.example.sunny.mynote.data.NoteItem;

进口的java.util.List;


公共类MainActivity扩展ListActivity {

    公共静态最终诠释EDITOR_ACRIVITY_REQUEST = 1001;
    公共静态最终诠释SETTINGS_REQUEST = 1003;
    私有静态最终诠释MENU_DELETE_ID = 1002;
    私人诠释currentNoteId;
    私人NoteDataSource数据源;
    名单< NoteItem> notesList;


    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        registerForContextMenu(getListView());

        数据源=新NoteDataSource(本);

        refreshDisplay();
    }

    私人无效refreshDisplay(){
        notesList = datasource.findAll();
        ArrayAdapter< NoteItem>适配器=
                新的ArrayAdapter< NoteItem>(这一点,R.layout.list_item_layout,notesList);
        setListAdapter(适配器);
    }


    //添加操作的操作栏
    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气操作栏中使用的菜单项
        MenuInflater充气= getMenuInflater();
        inflater.inflate(R.menu.menu_main,菜单);
        返回super.onCreateOptionsMenu(菜单);
    }
    //响应操作按钮
    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //操作栏上的项目手柄presses
        开关(item.getItemId()){
            案例R.id.action_create:
                createNote();
                返回true;
            案例R.id.action_settings:
                设置();
                返回true;
            默认:
                返回super.onOptionsItemSelected(项目);
        }
    }

    私人无效createNote()
    {
        NoteItem注= NoteItem.getNew();
        意向意图=新的意图(这一点,NoteEditorActivity.class);
        intent.putExtra(钥匙,note.getKey());
        intent.putExtra(文本,note.getText());
        startActivityForResult(意向,EDITOR_ACRIVITY_REQUEST);
    }

    私人无效设置()
    {
        意图int​​ent2 =新的意图(这一点,Settings.class);
        startActivityForResult(intent2,SETTINGS_REQUEST);
    }


    @覆盖
    保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        NoteItem注= notesList.get(位置);
        意向意图=新的意图(这一点,NoteEditorActivity.class);
        intent.putExtra(钥匙,note.getKey());
        intent.putExtra(文本,note.getText());
        startActivityForResult(意向,EDITOR_ACRIVITY_REQUEST);
    }

    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == EDITOR_ACRIVITY_REQUEST和放大器;&安培;结果code == RESULT_OK)
        {
            overridePendingTransition(R.anim.fadein,R.anim.fadeout);
            NoteItem注=新NoteItem();
            note.setKey(data.getStringExtra(钥匙));
            note.setText(data.getStringExtra(文本));
            datasource.update(注);
            refreshDisplay();
        }
    }

    @覆盖
    公共无效onCreateContextMenu(文本菜单菜单,视图V,ContextMenu.ContextMenuInfo menuInfo){

        AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)menuInfo;
        currentNoteId =(INT)info.id;
        menu.add(0,MENU_DELETE_ID,0,删除);
    }

    @覆盖
    公共布尔onContextItemSelected(菜单项项){

        如果(item.getItemId()== MENU_DELETE_ID)
        {
            NoteItem注意= notesList.get(currentNoteId);
            datasource.remove(注);
            refreshDisplay();
        }

        返回super.onContextItemSelected(项目);
    }
}
 

menu.xml文件

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s工具:上下文=MainActivity。>
    <项目机器人:ID =@ + ID / action_create机器人:标题=@字符串/ action_create
        机器人:图标=@可绘制/ create_note
        机器人:orderInCategory =100的应用程序:showAsAction =ifRoom/>
    <项目机器人:ID =@ + ID / action_settings机器人:标题=@字符串/ action_settings
        机器人:orderInCategory =100的应用程序:showAsAction =ifRoom安卓图标=@可绘制/ ic_settings/>
< /菜单>
 
为什么android添加button显示不出来

解决方案

加入menu.xml文件应用程序:showAsAction =ifRoom是这样的:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s工具:上下文=主。>

    <项目机器人:ID =@ + ID /粘​​贴机器人:标题=粘贴
        机器人:orderInCategory =100
        应用程序:showAsAction =ifRoom/>
< /菜单>
 

,并在主:

  //添加操作的操作栏
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气操作栏中使用的菜单项
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions,菜单);
    返回super.onCreateOptionsMenu(菜单);
}
//响应操作按钮
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //操作栏上的项目手柄presses
    开关(item.getItemId()){
        案例R.id.action_search:
            OpenSearch的();
            返回true;
        案例R.id.action_settings:
            openSettings();
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
    }
}
 

看到这样的: https://开头developer.android.com/training/basics/actionbar/adding-buttons.html

I'm implementing an action bar for my app which has 2 menu buttons : Add and Settings. In Designer view the 2 buttons still apprear like normal. However when I run the app 2 buttons does not show up at all. Please help

Below are my code:

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>

解决方案

add in menu.xml app:showAsAction="ifRoom" 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=".Main">

    <item android:id="@+id/paste" android:title="Paste"
        android:orderInCategory="100" 
        app:showAsAction="ifRoom" />
</menu>

and in main:

//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.main_activity_actions, 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_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

see this: https://developer.android.com/training/basics/actionbar/adding-buttons.html