菜单项不会在actiobar显示 [英] Menu items won't show up in the actiobar

查看:110
本文介绍了菜单项不会在actiobar显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新: 它的工作原理选项卡上,但是在手机上它的重叠由于某种原因,

我有一个关于该菜单项的android一个问题使用的动作条,我有一个自定义的背景为我actiobar(图片),在此设置在code。那块code正常工作。但是,当我尝试添加菜单项,它失败。

我要的很简单,我想有一个按钮,上面写着保存产品为最喜爱的在屏幕的底部,像底动作条。但是,当我尝试添加按钮菜单项不会显示出来,没有什么变化。

日志告诉我onCreateOptionsMenu()方法实际上是在执行。任何想法?

这是该活动我想要的菜单:

 包com.x;


进口的java.util.ArrayList;
进口的java.util.HashMap;

进口android.app.ActionBar;
进口android.app.ActionBar.Tab;
进口android.app.Activity;
进口android.app.LocalActivityManager;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.drawable.BitmapDrawable;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.TabHost;
进口android.widget.TabHost.TabSpec;
进口android.widget.TextView;

进口com.x.R;
进口com.x.products.fragments *。
进口com.x.tasks.ZoekQueryTask;
进口com.x.util.Helper;
进口com.x.util.UnscaledBitmapLoader;


公共类ProductActivity延伸活动{

    私人包保存;

    / **第一次创建活动时调用。 * /
    @燮pressWarnings(rawtypes)
    @覆盖
    公共无效的onCreate(包savedInstanceState){

        super.onCreate(savedInstanceState);
        的setContentView(R.layout.product);

        @燮pressWarnings(德precation)
        LocalActivityManager经理=新LocalActivityManager(这一点,假);

        助手帮手=新助手();
        节省= NULL;
        字符串ID =(字符串)getIntent()getExtras()获得(ID)。;
        字符串得分=(字符串)getIntent()getExtras()获得(分数)。;
        字符串配置文件ID =(字符串)getIntent()getExtras()获得(配置文件ID)。;

   //字符串ID =138946;
    //字符串配置文件ID =28;
    //串得分=100%;
        查询字符串=SELECT * FROM产品WHERE ID =+ ID;
        ArrayList的< HashMap的>结果= NULL;

        尝试 {
            ZoekQueryTask zoekQueryTask =新ZoekQueryTask(查询);
            结果= zoekQueryTask.getResults();
        }

        赶上(例外五){
            Log.e(Afgevangen错误,e.getMessage());
        }



        TabHost TB =(TabHost)findViewById(R.id.tabs);
        manager.dispatchCreate(savedInstanceState);
        tb.setup(经理);



        意向信息=新的意图();
        info.setClass(这一点,InfoFragment.class);
        info.putExtra(结果,结果);
        info.putExtra(配置文件ID,配置文件ID);

        则tabspec tsInfo = tb.newTabSpec(信息);
        tsInfo.setContent(信息);
        tsInfo.setIndicator(信息);



        意向规格=新意图();
        specs.setClass(这一点,SpecsFragment.class);
        specs.putExtra(结果,结果);

        则tabspec tsSpecs = tb.newTabSpec(规范);
        tsSpecs.setContent(规格);
        tsSpecs.setIndicator(规范);



        意图prijs =新意图();
        prijs.setClass(这一点,PrijsFragment.class);
        prijs.putExtra(结果,结果);

        则tabspec tsPrijs = tb.newTabSpec(prijs大奖);
        tsPrijs.setContent(prijs);
        tsPrijs.setIndicator(prijs大奖);

        则tabspec tsFotos = tb.newTabSpec(照片);
        tsFotos.setContent(信息);
        tsFotos.setIndicator(照片);


        tb.addTab(tsInfo);
        tb.addTab(tsSpecs);
        tb.addTab(tsPrijs);
        tb.addTab(tsFotos);



            最后的动作条动作条= getActionBar();
            actionBar.setDisplayShowHomeEnabled(假);
            actionBar.setDisplayShowTitleEnabled(假);


            位图BM = UnscaledBitmapLoader.loadFromResource(getResources(),R.drawable.logo,NULL);
            BitmapDrawable背景=新BitmapDrawable(this.getResources(),BM);
            background.setTileModeX(android.graphics.Shader.TileMode.CLAMP);
            actionBar.setBackgroundDrawable(背景);



            TextView的nameText =(TextView中)findViewById(R.id.name);
            TextView的scoreText =(TextView中)findViewById(R.id.score);

            nameText.setText(result.get(0)获得(姓名)的toString());
            scoreText.setText(分);

    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        MenuInflater充气= getMenuInflater();
        Log.i(你好,海);
        inflater.inflate(R.layout.product_menu,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理项目选择
        开关(item.getItemId()){
            案例R.id.save:
                Log.i(菜单,werkt);
                返回true;
            默认:
                返回super.onOptionsItemSelected(项目);
        }
    }

}
 

这是菜单的XML:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <项目机器人:ID =@ + ID /保存
          机器人:标题=Opslaan在favorieten>

    < /项目>
< /菜单>
 

解决方案

如果你想显示在操作栏中的菜单项,将安卓showAsAction的属性在product_menu.xml您的项目;

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目
        机器人:ID =@ + ID / ITEM1
        机器人:标题=保存
        机器人:showAsAction =总是/>
< /菜单>
 

您可以将此属性设置为:

  • ifRoom
  • 从来没有
  • withText
  • 总是
  • collapseActionView

我希望这可以帮助你!

斯特芬

UPDATE: It works on the tab, but on the phone it's overlayed for some reason

I have a question regarding the menu items android uses for the ActionBar, I have a custom background for my actiobar (a picture) and set it in the code here. That piece of code works fine. But when I try to add a menu item, it fails.

What I want is fairly simple, I want a button that says "save product as favorite" at the bottom of the screen, like the bottom-actionbar. But when I try to add the button as menu item it won't show up, nothing changes.

The log tells me the onCreateOptionsMenu() method is in fact executed. Any ideas?

This is the activity I want the menu for:

package com.x;


import java.util.ArrayList;
import java.util.HashMap;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.x.R;
import com.x.products.fragments.*;
import com.x.tasks.ZoekQueryTask;
import com.x.util.Helper;
import com.x.util.UnscaledBitmapLoader;


public class ProductActivity extends Activity {

    private Bundle save;

    /** Called when the activity is first created. */
    @SuppressWarnings("rawtypes")
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.product);

        @SuppressWarnings("deprecation")
        LocalActivityManager manager = new LocalActivityManager(this, false);

        Helper helper = new Helper();
        save = null;
        String id = (String) getIntent().getExtras().get("id");
        String score = (String) getIntent().getExtras().get("score");
        String profileId = (String) getIntent().getExtras().get("profileId");

   //     String id = "138946";
    //    String profileId = "28";
    //    String score = "100%";
        String query  = "SELECT * FROM products WHERE id = "+id;
        ArrayList<HashMap> result = null;

        try {
            ZoekQueryTask zoekQueryTask = new ZoekQueryTask(query);
            result = zoekQueryTask.getResults();
        }

        catch(Exception e) {
            Log.e("Afgevangen error", e.getMessage());
        }



        TabHost tb = (TabHost) findViewById(R.id.tabs);
        manager.dispatchCreate(savedInstanceState);
        tb.setup(manager);



        Intent info = new Intent();
        info.setClass(this, InfoFragment.class);
        info.putExtra("result", result);
        info.putExtra("profileId", profileId);

        TabSpec tsInfo = tb.newTabSpec("info");
        tsInfo.setContent(info);
        tsInfo.setIndicator("info");



        Intent specs = new Intent();
        specs.setClass(this, SpecsFragment.class);
        specs.putExtra("result", result);

        TabSpec tsSpecs = tb.newTabSpec("specs");
        tsSpecs.setContent(specs);
        tsSpecs.setIndicator("specs");



        Intent prijs = new Intent();
        prijs.setClass(this, PrijsFragment.class);
        prijs.putExtra("result", result);

        TabSpec tsPrijs = tb.newTabSpec("Prijs");
        tsPrijs.setContent(prijs);
        tsPrijs.setIndicator("Prijs");

        TabSpec tsFotos = tb.newTabSpec("Fotos");
        tsFotos.setContent(info);
        tsFotos.setIndicator("Fotos");


        tb.addTab(tsInfo); 
        tb.addTab(tsSpecs);
        tb.addTab(tsPrijs);
        tb.addTab(tsFotos);



            final ActionBar actionBar = getActionBar(); 
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);


            Bitmap bm = UnscaledBitmapLoader.loadFromResource(getResources(), R.drawable.logo, null);
            BitmapDrawable background = new BitmapDrawable(this.getResources(), bm);
            background.setTileModeX(android.graphics.Shader.TileMode.CLAMP); 
            actionBar.setBackgroundDrawable(background);



            TextView nameText = (TextView) findViewById(R.id.name);
            TextView scoreText = (TextView) findViewById(R.id.score);

            nameText.setText(result.get(0).get("name").toString());
            scoreText.setText(score);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        Log.i("hallo", "hoi");
        inflater.inflate(R.layout.product_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.save:
                Log.i("menu", "werkt");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

This is the menu XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/save"
          android:title="Opslaan in favorieten" >

    </item>
</menu>

解决方案

If you want to show your menu item in the action bar, set the android:showAsAction property of your item in product_menu.xml;

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:id="@+id/item1" 
        android:title="Save"
        android:showAsAction="always" />
</menu>

You can set this property to:

  • ifRoom
  • never
  • withText
  • always
  • collapseActionView

I hope this helps you!

Steffen

这篇关于菜单项不会在actiobar显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆