无法更改导航抽屉中ActionBar中应用图标旁边的向上插入符号

时间:2013-06-01 08:17:01

标签: android

我目前正在尝试按照Google IO 2013指南实施新的NavigationDrawer。我正在使用ActionbarSherlock。代码运行良好。唯一的问题是,当拉出导航抽屉时,我无法切换应用程序图标旁边的“向上”插入符号。我正在粘贴下面的全部代码。请帮助。

public class MainActivity extends SherlockFragmentActivity {
   private DrawerLayout mDrawerLayout;
   private ListView mDrawerList;
   private ActionBarDrawerToggle mDrawerToggle;

   private CharSequence mDrawerTitle;
   private CharSequence mTitle;
   private String[] mPlanetTitles;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      mTitle = mDrawerTitle = getTitle();
      mPlanetTitles = getResources().getStringArray(R.array.planets_array);
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      mDrawerList = (ListView) findViewById(R.id.drawer);

      mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
      mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
      mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

      mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
      mDrawerLayout, /* DrawerLayout object */
      R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
      R.string.drawer_open, /* "open drawer" description for accessibility */
      R.string.drawer_close /* "close drawer" description for accessibility */
      ) {
         public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            supportInvalidateOptionsMenu(); 
         }

         public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            supportInvalidateOptionsMenu(); 
         }
      };

      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setHomeButtonEnabled(true);


      mDrawerLayout.setDrawerListener(mDrawerToggle);

      if (savedInstanceState == null) {
         selectItem(0);
      }

      new Thread(new Runnable() {

        @Override
        public void run() {
             prefs = getPreferences(MODE_PRIVATE);
             opened = prefs.getBoolean(OPENED_KEY, false);
             if(opened == false)
             {
                     mDrawerLayout.openDrawer(mDrawerList);
             }
        }
    }).start();
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getSupportMenuInflater();
      inflater.inflate(R.menu.main, menu);
      return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onPrepareOptionsMenu(Menu menu) {
      boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
      menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
      return super.onPrepareOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(final MenuItem item) {
      if (mDrawerToggle.onOptionsItemSelected(getMenuItem(item))) { 
         return true; 
      }

      switch (item.getItemId()) {
         case R.id.action_websearch:
            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
            intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
            if (intent.resolveActivity(getPackageManager()) != null) {
               startActivity(intent);
            } else {
               Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
            }
            return true;
         default:
            return super.onOptionsItemSelected(item);
      }
   }

   private android.view.MenuItem getMenuItem(final MenuItem item) {
      return new android.view.MenuItem() {
         @Override
         public int getItemId() {
            return item.getItemId();
         }

         public boolean isEnabled() {
            return true;
         }

         @Override
         public boolean collapseActionView() {
            return false;
         }

         @Override
         public boolean expandActionView() {
            return false;
         }

         @Override
         public ActionProvider getActionProvider() {
            return null;
         }

         @Override
         public View getActionView() {

            return null;
         }

         @Override
         public char getAlphabeticShortcut() {

            return 0;
         }

         @Override
         public int getGroupId() {

            return 0;
         }

         @Override
         public Drawable getIcon() {

            return null;
         }

         @Override
         public Intent getIntent() {

            return null;
         }

         @Override
         public ContextMenuInfo getMenuInfo() {

            return null;
         }

         @Override
         public char getNumericShortcut() {

            return 0;
         }

         @Override
         public int getOrder() {

            return 0;
         }

         @Override
         public SubMenu getSubMenu() {

            return null;
         }

         @Override
         public CharSequence getTitle() {

            return null;
         }

         @Override
         public CharSequence getTitleCondensed() {

            return null;
         }

         @Override
         public boolean hasSubMenu() {

            return false;
         }

         @Override
         public boolean isActionViewExpanded() {

            return false;
         }

         @Override
         public boolean isCheckable() {

            return false;
         }

         @Override
         public boolean isChecked() {

            return false;
         }

         @Override
         public boolean isVisible() {

            return false;
         }

         @Override
         public android.view.MenuItem setActionProvider(ActionProvider actionProvider) {

            return null;
         }

         @Override
         public android.view.MenuItem setActionView(View view) {

            return null;
         }

         @Override
         public android.view.MenuItem setActionView(int resId) {

            return null;
         }

         @Override
         public android.view.MenuItem setAlphabeticShortcut(char alphaChar) {

            return null;
         }

         @Override
         public android.view.MenuItem setCheckable(boolean checkable) {

            return null;
         }

         @Override
         public android.view.MenuItem setChecked(boolean checked) {

            return null;
         }

         @Override
         public android.view.MenuItem setEnabled(boolean enabled) {

            return null;
         }

         @Override
         public android.view.MenuItem setIcon(Drawable icon) {

            return null;
         }

         @Override
         public android.view.MenuItem setIcon(int iconRes) {

            return null;
         }

         @Override
         public android.view.MenuItem setIntent(Intent intent) {

            return null;
         }

         @Override
         public android.view.MenuItem setNumericShortcut(char numericChar) {

            return null;
         }

         @Override
         public android.view.MenuItem setOnActionExpandListener(OnActionExpandListener listener) {

            return null;
         }

         @Override
         public android.view.MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {

            return null;
         }

         @Override
         public android.view.MenuItem setShortcut(char numericChar, char alphaChar) {

            return null;
         }

         @Override
         public void setShowAsAction(int actionEnum) {


         }

         @Override
         public android.view.MenuItem setShowAsActionFlags(int actionEnum) {

            return null;
         }

         @Override
         public android.view.MenuItem setTitle(CharSequence title) {

            return null;
         }

         @Override
         public android.view.MenuItem setTitle(int title) {

            return null;
         }

         @Override
         public android.view.MenuItem setTitleCondensed(CharSequence title) {

            return null;
         }

         @Override
         public android.view.MenuItem setVisible(boolean visible) {

            return null;
         }
      };
   }

   /* The click listner for ListView in the navigation drawer */
   private class DrawerItemClickListener implements ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         selectItem(position);
      }
   }

   private void selectItem(int position) {
      // update the main content by replacing fragments
      Fragment fragment = new PlanetFragment();
      Bundle args = new Bundle();
      args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
      fragment.setArguments(args);

      FragmentManager fragmentManager = getSupportFragmentManager();
      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

      // update selected item and title, then close the drawer
      mDrawerList.setItemChecked(position, true);
      setTitle(mPlanetTitles[position]);
      mDrawerLayout.closeDrawer(mDrawerList);
   }

   @Override
   public void setTitle(CharSequence title) {
      mTitle = title;
      getSupportActionBar().setTitle(mTitle);
   }

   /**
    * When using the ActionBarDrawerToggle, you must call it during
    * onPostCreate() and onConfigurationChanged()...
    */

   @Override
   protected void onPostCreate(Bundle savedInstanceState) {
      super.onPostCreate(savedInstanceState);
      // Sync the toggle state after onRestoreInstanceState has occurred.
      mDrawerToggle.syncState();
   }

   @Override
   public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      // Pass any configuration change to the drawer toggls
      mDrawerToggle.onConfigurationChanged(newConfig);
   }

   /**
    * Fragment that appears in the "content_frame", shows a planet
    */
   public static class PlanetFragment extends SherlockFragment {
      public static final String ARG_PLANET_NUMBER = "planet_number";

      public PlanetFragment() {
         // Empty constructor required for fragment subclasses
      }

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
         int i = getArguments().getInt(ARG_PLANET_NUMBER);
         String planet = getResources().getStringArray(R.array.planets_array)[i];

      // change int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName());
         int imageId = getResources().getIdentifier("tempmap", "drawable", getActivity().getPackageName());
         // change ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
         ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
         // change  getActivity().setTitle(planet);
         getActivity().setTitle("MapView");
         return rootView;
      }
   }
}

4 个答案:

答案 0 :(得分:11)

如果你不需要移动3行(比如在gmail应用程序中),你可以添加:

<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>

在您的活动主题中。我更喜欢在我的应用程序中使用另一个库。

ic_drawer可以从这里下载: http://developer.android.com/training/implementing-navigation/nav-drawer.html

答案 1 :(得分:11)

尝试在切换抽屉上调用syncState,如下所示:

    @Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

//your ActionBarDrawerToggle is below
    mDrawerToggle.syncState();
}

或者在实例化之后。

答案 2 :(得分:3)

我遇到了同样的问题并对该主题进行了一些研究。在较旧的SDK上使插入符号正常工作似乎并不容易。简而言之,有一个人有一个基于反思的运行示例实现:

https://github.com/nicolasjafelle/SherlockNavigationDrawer

我测试了它,它对我来说非常适合,即使在Android 2.x上也是如此!

答案 3 :(得分:0)

我在4.x设备中删除了插入符号,但没有删除2.x设备。如果您需要,存储库就在这里: https://github.com/bcrider/NavigationDrawerSherlocked

这只是Google提供的样本,我使用ActionBarSherlock进行了修改。我修复了样本中的方法(onOptionsItemSelected)的问题,该方法最初阻止了Tapped图标在ActionBarSherlock集成到应用程序后执行任何操作。

希望这有帮助!