【问题标题】:Action bar icon working fine on one activity not on other操作栏图标在一项活动上正常工作而不是在其他活动上
【发布时间】:2014-09-25 05:34:36
【问题描述】:

我将以下 menu.xml 用于操作栏菜单,并且它在我的主要活动中运行良好

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_drawer"
    android:orderInCategory="0"
    android:title="navigation"
    app:showAsAction="always"/>
<item
    android:id="@+id/action_settings"
    android:icon="@drawable/settings"
    android:orderInCategory="0"
    android:title="Settings"
    app:showAsAction="always">
</item>

但是当我在其他活动菜单上使用相同的 menu.xml 时,菜单图标不会出现。这是我的主要活动的快照

现在这里是我的其他活动的快照。 为什么我会遇到这个问题,请让我知道您对此的反馈。

这是我的 onCreateOptionsMenu 代码

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.test,menu);
        return true;
    }

我将这段代码用于这两项活动。

这是我的 SecondActivity 的代码

package com.example.actionbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

    }
     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.test, menu);
            return true;
        }

        @Override
        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();
            if (id == R.id.action_refresh) {
                 Toast.makeText(this, "navigation selected", Toast.LENGTH_SHORT)
                 .show();
            }
            if(id == R.id.action_settings){
                 Toast.makeText(this, "settings selected", Toast.LENGTH_SHORT)
                 .show();
            }
            return super.onOptionsItemSelected(item);
        }
}

【问题讨论】:

  • 您能否为这两个活动发布您的 onCreateOptionsMenu。
  • 发布onOptionsItemSelected 方法。如果你还没有覆盖这个方法,那么你应该这样做。
  • 我看不出有什么问题,你能发布第二个活动的完整代码吗?
  • 是的,我已经发布了第二个活动的完整代码,在第一个活动中它可以找到但在第二个活动中我仍然面临同样的问题
  • 我注意到你在一个地方使用 R.menu.action_menu,而在另一个地方你正在使用 R.menu.test。会不会是个问题?

标签: android xml android-layout android-activity


【解决方案1】:

您是否在其他活动中使用过 onCreateOptionsMenu 和 onOptionsItemSelected 方法,如果没有,请尝试...

【讨论】:

  • menu.findItem(R.id.action_refresh).setVisible(true); menu.findItem(R.id.action_settings).setVisible(true) 在你的 onCreateOptionsMenu 的其他活动中试试这个
  • @pushpendra menu.findItem(R.id.action_refresh).setVisible(true); menu.findItem(R.id.action_settings).setVisible(true);在其他活动的 onCreateOptionsMenu 中试试这个
  • setHasOptionsMenu(true);在该活动的 oncreate 方法中使用它。你有没有使用上面的代码 getMenuInflater().inflate(R.menu.action_menu,menu); ??
  • 我已经看到您编辑的代码更正了您的 R.menu.---- 代替破折号,您必须使用您正在使用两项刷新和设置的主 xml 的名称。它必须在两个活动 oncreateoptionmenu 方法和你想使用的地方都相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
相关资源
最近更新 更多