菜单项不会显示在操作栏中

时间:2012-05-07 08:54:34

标签: android android-activity menu android-actionbar

更新 它适用于选项卡,但在手机上它由于某种原因而叠加

我有一个关于android用于ActionBar的菜单项的问题,我有一个自定义背景我的actiobar(图片)并在此处的代码中设置它。这段代码工作得很好。但是当我尝试添加菜单项时,它会失败。

我想要的是相当简单,我想要一个按钮,在屏幕底部显示“将产品保存为收藏”,就像底部操作栏一样。但是当我尝试将按钮添加为菜单项时,它不会显示,没有任何变化。

日志告诉我onCreateOptionsMenu()方法实际上已执行。有什么想法吗?

这是我想要菜单的活动:

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);
        }
    }

}

这是菜单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>

2 个答案:

答案 0 :(得分:0)

我认为你需要解决这个问题:

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

而不是R.layout.product_menu,应该引用R.menu.your_desired_menu

答案 1 :(得分:0)

如果要在操作栏中显示菜单项,请在product_menu.xml中设置项目的android:showAsAction属性;

<?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>

您可以将此属性设置为:

  • ifRoom
  • 从未
  • withText
  • 总是
  • collapseActionView

我希望这可以帮到你!

斯特芬