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

前端之家收集整理的这篇文章主要介绍了android – 无法在导航抽屉的ActionBar中更改应用程序图标旁边的向上插入符号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在尝试按照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",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;
      }
   }
}

解决方法

如果你不需要移动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

原文链接:https://www.f2er.com/android/317470.html

猜你在找的Android相关文章