2014-06-24 52 views
0

在我的Android應用程序中,我試圖在我的FragmentActivity中將圖標添加到ActionBar;但是,它仍然是空白的。使用實現ActionBar.TabListener的FragmentActivity編輯ActionBar

我想動作條有從block.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=".SetupActivity" > 



    <item android:id="@+id/action_overflow" 
      android:title="@string/action_settings" 
      android:orderInCategory="100" 
      android:icon="@drawable/ic_action_overflow" 
      app:showAsAction="always" /> 

    <item android:id="@+id/action_search" 
      android:title="@string/search" 
      android:orderInCategory="200" 
      android:icon="@drawable/ic_action_search" 
      app:showAsAction="always" /> 
</menu> 

兩個圖標我想按照這個:onCreateOptionsMenu not being called on FragmentActivity when run on phone version

這裏是延伸FragmentActivity我BlockActivity類。儘管有onCreateOptionsMenu方法,但我不確定爲什麼無法編輯ActionBar。

package com.spicycurryman.getdisciplined10.app; 

import android.annotation.TargetApi; 
import android.app.FragmentTransaction; 
import android.os.Build; 
import android.os.Bundle; 
import android.app.ActionBar; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.view.Menu; 
import android.view.MenuInflater; 

import info.androidhive.tabsswipe.adapter.TabsPagerAdapter; 


// 



/** 
* Created by Spicycurryman on 6/17/14. 
*/ 
public class BlockActivity extends FragmentActivity implements 
     ActionBar.TabListener { 

    private ViewPager viewPager; 
    private TabsPagerAdapter mAdapter; 
    private ActionBar actionBar; 
    // Tab titles 
    private String[] tabs = {"Installed", "System", "Custom"}; 

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.block_apps); 


     // Initilization 
     viewPager = (ViewPager) findViewById(R.id.pager); 
     actionBar = getActionBar(); 
     mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 

     viewPager.setAdapter(mAdapter); 
     actionBar.setHomeButtonEnabled(false); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Adding Tabs 
     for (String tab_name : tabs) { 
      actionBar.addTab(actionBar.newTab().setText(tab_name) 
        .setTabListener(this)); 
     } 

     /** 
     * on swiping the viewpager make respective tab selected 
     * */ 
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int position) { 
       // on changing the page 
       // make respected tab selected 
       actionBar.setSelectedNavigationItem(position); 
      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
      } 

      @Override 
      public void onPageScrollStateChanged(int arg0) { 
      } 
     }); 

    } 

    //Here is onCreateOptionsMenu method that should work 

    public boolean onCreateOptionsMenu(Menu menu){ 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.block, menu); 
     return true; 
    } 





    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
     viewPager.setCurrentItem(tab.getPosition()); 

    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 

    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 

    } 




} 

onCreateOptionsMenu應該在單獨的Fragments中實現嗎?

回答

0

您沒有撥打super.onCreateOptionsMenu(menu);裏面的onCreateOptionsMenu方法,這就是爲什麼它仍然是空白。

解決方案:

public boolean onCreateOptionsMenu(Menu menu){ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.block, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

添加此還有:

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    return super.onPrepareOptionsMenu(menu); 
} 

添加動作,以及:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_overflow) { 
     return true; 
    } else if(id == R.id.action_search) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

不幸的是,它仍然沒有顯示出來。 –

+0

@XiJiaopin editted將上面的代碼添加到您的actovoity –

+0

它仍然沒有顯示出問題仍然在onCreateOptionsMenu? –

相關問題