更改箭头图标在抽屉菜单? [英] Change the Arrow Icon in the Drawer Menu?

查看:114
本文介绍了更改箭头图标在抽屉菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android的工作室。我创建了一个抽屉式导航栏的新活动。 但是,它表示的< - 代替汉堡包图标

如何修改我的code键更改< - 对汉堡图标????

 进口android.app.Activity;
进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.content.Context;
进口android.os.Build;
进口android.os.Bundle;
进口android.view.Gravity;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.support.v4.widget.DrawerLayout;
进口android.widget.ArrayAdapter;
进口android.widget.TextView;


 公共类MainActivity扩展ActionBarActivity
    实现NavigationDrawerFragment.NavigationDrawerCallbacks {

/ **
 *片段管理导航抽屉的行为,相互作用和presentation。
 * /
私人NavigationDrawerFragment mNavigationDrawerFragment;

/ **
 *用于存储最后屏幕标题。对于使用{@link #restoreActionBar()}。
 * /
私人的CharSequence mTitle;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    mNavigationDrawerFragment =(NavigationDrawerFragment)
            。getSupportFragmentManager()findFragmentById(R.id.navigation_drawer);
    mTitle =的getTitle();

    //设置抽屉。
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout)findViewById(R.id.drawer_layout));
}

@覆盖
公共无效onNavigationDrawerItemSelected(INT位置){
    //更新替换片段的主要内容
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container,PlaceholderFragment.newInstance(位置+ 1))
            。承诺();
}

公共无效onSectionAttached(INT编号){
    开关(数){
        情况1:
            mTitle =的getString(R.string.title_section1);
            打破;
        案例2:
            mTitle =的getString(R.string.title_section2);
            打破;
        案例3:
            mTitle =的getString(R.string.title_section3);
            打破;
    }
}

公共无效restoreActionBar(){
    动作条动作条= getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setLogo(R.drawable.ic_drawer);
    actionBar.setDisplayShowTitleEnabled(真正的);
    actionBar.setTitle(mTitle);
}


@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    如果(!mNavigationDrawerFragment.isDrawerOpen()){
        //只显示有关该屏幕中的操作栏项目
        //如果抽屉没有显示。否则,让抽屉
        //决定如何操作栏中显示。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        restoreActionBar();
        返回true;
    }
    返回super.onCreateOptionsMenu(菜单);
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。将操作栏
    //自动在主/向上按钮操作的点击,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT的id = item.getItemId();

    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }

    返回super.onOptionsItemSelected(项目);
}

/ **
 *包含一个简单的视图的占位符片段。
 * /
公共静态类PlaceholderFragment扩展片段{
    / **
     *片段参数重新presenting本章节号
     * 分段。
     * /
    私有静态最后弦乐ARG_SECTION_NUMBER =section_number标;

    / **
     *对于给定的部分中返回此片段的一个新实例
     * 数。
     * /
    公共静态PlaceholderFragment的newInstance(INT sectionNumber){
        PlaceholderFragment片段=新PlaceholderFragment();
        捆绑的args =新包();
        args.putInt(ARG_SECTION_NUMBER,sectionNumber);
        fragment.setArguments(参数);
        返回片段;
    }

    公共PlaceholderFragment(){
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
        返回rootView;
    }

    @覆盖
    公共无效onAttach(活动活动){
        super.onAttach(活动);
        ((MainActivity)活动).onSectionAttached(
                。getArguments()调用getInt(ARG_SECTION_NUMBER));
    }
}
 

}

 包com.mw.alahdal.amjad.menudrawer;

  进口android.support.v7.app.ActionBarActivity;
  进口android.app.Activity;
  进口android.support.v7.app.ActionBar;
  进口android.support.v4.app.Fragment;
  进口android.support.v4.app.ActionBarDrawerToggle;
  进口android.support.v4.view.GravityCompat;
  进口android.support.v4.widget.DrawerLayout;
  进口android.content.Shared preferences;
  进口android.content.res.Configuration;
  进口android.os.Bundle;
  。进口的Andr​​oid preference preferenceManager。
  进口android.view.LayoutInflater;
  进口android.view.Menu;
  进口android.view.MenuInflater;
  进口android.view.MenuItem;
  进口android.view.View;
  进口android.view.ViewGroup;
  进口android.widget.AdapterView;
  进口android.widget.ArrayAdapter;
  进口android.widget.ListView;
  进口android.widget.Toast;


 公共类NavigationDrawerFragment扩展片段{

/ **
 *请记住所选项目的位置。
 * /
私有静态最后弦乐STATE_SELECTED_POSITION =selected_navigation_drawer_position;

/ **
 *根据设计准则,应手动显示上推出了抽屉里,直到用户
 *扩展它。这种共享preference跟踪此。
 * /
私有静态最后弦乐preF_USER_LEARNED_DRAWER =navigation_drawer_learned;

/ **
 *一个指向当前回调实例(活动)。
 * /
私人NavigationDrawerCallbacks mCallbacks;

/ **
 *是关系操作栏的抽屉式导航辅助部件。
 * /
私人ActionBarDrawerToggle mDrawerToggle;

私人DrawerLayout mDrawerLayout;
私人的ListView mDrawerListView;
私人查看mFragmentContainerView;

私人诠释mCurrentSelectedPosition = 0;
私人布尔mFromSavedInstanceState;
私人布尔mUserLearnedDrawer;

公共NavigationDrawerFragment(){
}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //读取在指示用户是否已经证实的认识
    // 抽屉。见preF_USER_LEARNED_DRAWER了解详细信息。
    共享preferences SP = preferenceManager.getDefaultShared preferences(getActivity());
    mUserLearnedDrawer = sp.getBoolean(preF_USER_LEARNED_DRAWER,假);

    如果(savedInstanceState!= NULL){
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        mFromSavedInstanceState = TRUE;
    }

    //选择任默认项(0)或最后选择的项目。
    选择信息(mCurrentSelectedPosition);
}

@覆盖
公共无效onActivityCreated(包savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    //表明该片段想影响的一组操作栏中的行动。
    setHasOptionsMenu(真正的);
    }

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                         捆绑savedInstanceState){
    mDrawerListView =(ListView控件)inflater.inflate(
            R.layout.fragment_navigation_drawer,集装箱,假);
    mDrawerListView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
            选择信息(位置);
        }
    });
    mDrawerListView.setAdapter(新ArrayAdapter<字符串>(
            getActionBar()。getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            新的String [] {
                    的getString(R.string.title_section1)
                    的getString(R.string.title_section2)
                    的getString(R.string.title_section3)
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition,真正的);
    返回mDrawerListView;
}

公共布尔isDrawerOpen(){
    返回mDrawerLayout = NULL和放大器;!&安培; mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}

/ **
 *该片段的用户必须调用这个方法来设置导航抽屉相互作用。
 *
 *参数fragmentId Android的:ID此片段在其活动的布局。
 *参数drawerLayout包含该片段的UI的DrawerLayout。
 * /
公共无效设置(INT fragmentId,DrawerLayout drawerLayout){
    mFragmentContainerView = getActivity()findViewById(fragmentId)。
    mDrawerLayout = drawerLayout;

    抽屉打开时//设置自定义影子覆盖的主要内容
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
    //设置抽屉的列表视图项,然后单击监听器

    动作条动作条= getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(真正的);
    actionBar.setHomeButtonEnabled(真正的);

    // ActionBarDrawerToggle联系在一起的适当交互
    //导航抽屉和操作栏中的应用程序图标之间。
    mDrawerToggle =新ActionBarDrawerToggle(
            getActivity()/ *主机活动* /
            mDrawerLayout,/ * DrawerLayout对象* /
            R.drawable.ic_drawer,/ *导航抽屉图像,以取代上插入符* /
            R.string.navigation_drawer_open,/ *开放抽屉说明访问* /
            R.string.navigation_drawer_close / *关闭抽屉说明访问* /
    ){
        @覆盖
        公共无效onDrawerClosed(查看drawerView){
            super.onDrawerClosed(drawerView);
            如果(!isAdded()){
                返回;
            }

            。getActivity()supportInvalidateOptionsMenu();在prepareOptionsMenu //调用()
        }

        @覆盖
        公共无效onDrawerOpened(查看drawerView){
            super.onDrawerOpened(drawerView);
            如果(!isAdded()){
                返回;
            }

            如果(!mUserLearnedDrawer){
                //用户手动打开抽屉;该标志存储prevent自动显示
                //在未来导航自动抽屉。
                mUserLearnedDrawer = TRUE;
                共享preferences SP = preferenceManager
                        .getDefaultShared preferences(getActivity());
                。sp.edit()putBoolean(preF_USER_LEARNED_DRAWER,真)。适用();
            }

            。getActivity()supportInvalidateOptionsMenu();在prepareOptionsMenu //调用()
        }
    };

    //如果用户没有学习关于抽屉,打开它,给他们介绍的抽屉里,
    //每导航抽屉式设计的指导方针。
    如果(mUserLearnedDrawer&安培;!&安培;!mFromSavedInstanceState){
        mDrawerLayout.openDrawer(mFragmentContainerView);
    }

    //推迟code依赖于恢复previous实例状态。
    mDrawerLayout.post(新的Runnable(){
        @覆盖
        公共无效的run(){
            mDrawerToggle.syncState();
        }
    });

    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

私人无效选择信息(INT位置){
    mCurrentSelectedPosition =位置;
    如果(mDrawerListView!= NULL){
        mDrawerListView.setItemChecked(位置,真正的);
    }
    如果(mDrawerLayout!= NULL){
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    如果(mCallbacks!= NULL){
        mCallbacks.onNavigationDrawerItemSelected(位置);
    }
}

@覆盖
公共无效onAttach(活动活动){
    super.onAttach(活动);
    尝试 {
        mCallbacks =(NavigationDrawerCallbacks)的活动;
    }赶上(ClassCastException异常E){
        抛出新的ClassCastException异常(活动必须实现NavigationDrawerCallbacks。);
    }
}

@覆盖
公共无效onDetach(){
    super.onDetach();
    mCallbacks = NULL;
}

@覆盖
公共无效的onSaveInstanceState(包outState){
    super.onSaveInstanceState(outState);
    outState.putInt(STATE_SELECTED_POSITION,mCurrentSelectedPosition);
}

@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
    super.onConfigurationChanged(NEWCONFIG);
    //提出了新的配置抽屉切换组件。
    mDrawerToggle.onConfigurationChanged(NEWCONFIG);
}

@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
    //如果抽屉是打开的,显示在操作栏上的全球应用程序的行为。也可以看看
    // showGlobalContextActionBar,控制操作栏的左上角区域。
    如果(mDrawerLayout =空&安培;!&安培; isDrawerOpen()){
        inflater.inflate(R.menu.global,菜单);
        showGlobalContextActionBar();
    }
    super.onCreateOptionsMenu(菜单,充气);
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    如果(mDrawerToggle.onOptionsItemSelected(项目)){
        返回true;
    }

    如果(item.getItemId()== R.id.action_example){
        Toast.makeText(getActivity(),示例的动作。,Toast.LENGTH_SHORT).show();
        返回true;
    }

    返回super.onOptionsItemSelected(项目);
}

/ **
 *每导航抽屉式设计的指导方针,更新操作栏,显示了全球应用程序
 *语境,而不仅仅是什么是当前屏幕。
 * /
私人无效showGlobalContextActionBar(){
    动作条动作条= getActionBar();
    actionBar.setDisplayShowTitleEnabled(真正的);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setTitle(R.string.app_name);
}

私有动作条getActionBar(){
    返程((ActionBarActivity)getActivity())getSupportActionBar()。
}

/ **
 *回调,使用此片段的所有活动都必须实现的接口。
 * /
公共静态接口NavigationDrawerCallbacks {
    / **
     *在导航抽屉一个项目被选中时调用。
     * /
    无效onNavigationDrawerItemSelected(INT位);
}
}
 

解决方案
  1. 使用android.support.v7.app.ActionBarDrawerToggle代替 android.support.v4.app.ActionBarDrawerToggle

  2. 您会得到一个错误的ActionBarDrawerToggle的构造。使用构造ActionBarDrawerToggle V7有4个参数。

  3. 另外添加到您的styles.xml

    <样式名称=AppTheme父=Theme.AppCompat.Light> <项目名称=drawerArrowStyle> @风格/ DrawerArrowStyle< /项目> < /风格> <样式名称=DrawerArrowStyle父=Widget.AppCompat.DrawerArrowToggle> <项目名称=spinBars>真< /项目> < /风格>

4.适用这个主题来在清单中的应用程序。 希望这有助于。

I am using Android Studio. And I created a new activity with a navigation drawer. But, It's showing the <- instead of the Hamburger Icon.

How can I modify my code to change <- to the Hamburger Icon????

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater; 
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;


 public class MainActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setLogo(R.drawable.ic_drawer);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

}

  package com.mw.alahdal.amjad.menudrawer;

  import android.support.v7.app.ActionBarActivity;
  import android.app.Activity;
  import android.support.v7.app.ActionBar;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.ActionBarDrawerToggle;
  import android.support.v4.view.GravityCompat;
  import android.support.v4.widget.DrawerLayout;
  import android.content.SharedPreferences;
  import android.content.res.Configuration;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.Toast;


 public class NavigationDrawerFragment extends Fragment {

/**
 * Remember the position of the selected item.
 */
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

/**
 * Per the design guidelines, you should show the drawer on launch until the user manually
 * expands it. This shared preference tracks this.
 */
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";

/**
 * A pointer to the current callbacks instance (the Activity).
 */
private NavigationDrawerCallbacks mCallbacks;

/**
 * Helper component that ties the action bar to the navigation drawer.
 */
private ActionBarDrawerToggle mDrawerToggle;

private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;

private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;

public NavigationDrawerFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Read in the flag indicating whether or not the user has demonstrated awareness of the
    // drawer. See PREF_USER_LEARNED_DRAWER for details.
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

    if (savedInstanceState != null) {
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        mFromSavedInstanceState = true;
    }

    // Select either the default item (0) or the last selected item.
    selectItem(mCurrentSelectedPosition);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Indicate that this fragment would like to influence the set of actions in the action bar.
    setHasOptionsMenu(true);
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });
    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}

public boolean isDrawerOpen() {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}

/**
 * Users of this fragment must call this method to set up the navigation drawer interactions.
 *
 * @param fragmentId   The android:id of this fragment in its activity's layout.
 * @param drawerLayout The DrawerLayout containing this fragment's UI.
 */
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
    mFragmentContainerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the navigation drawer and the action bar app icon.
    mDrawerToggle = new ActionBarDrawerToggle(
            getActivity(),                    /* host Activity */
            mDrawerLayout,                    /* DrawerLayout object */
            R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
            R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
            R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
    ) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) {
                return;
            }

            getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (!isAdded()) {
                return;
            }

            if (!mUserLearnedDrawer) {
                // The user manually opened the drawer; store this flag to prevent auto-showing
                // the navigation drawer automatically in the future.
                mUserLearnedDrawer = true;
                SharedPreferences sp = PreferenceManager
                        .getDefaultSharedPreferences(getActivity());
                sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
            }

            getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }
    };

    // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
    // per the navigation drawer design guidelines.
    if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
        mDrawerLayout.openDrawer(mFragmentContainerView);
    }

    // Defer code dependent on restoration of previous instance state.
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });

    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

private void selectItem(int position) {
    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mCallbacks = (NavigationDrawerCallbacks) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mCallbacks = null;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Forward the new configuration the drawer toggle component.
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // If the drawer is open, show the global app actions in the action bar. See also
    // showGlobalContextActionBar, which controls the top-left area of the action bar.
    if (mDrawerLayout != null && isDrawerOpen()) {
        inflater.inflate(R.menu.global, menu);
        showGlobalContextActionBar();
    }
    super.onCreateOptionsMenu(menu, inflater);
}

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

    if (item.getItemId() == R.id.action_example) {
        Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * Per the navigation drawer design guidelines, updates the action bar to show the global app
 * 'context', rather than just what's in the current screen.
 */
private void showGlobalContextActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setTitle(R.string.app_name);
}

private ActionBar getActionBar() {
    return ((ActionBarActivity) getActivity()).getSupportActionBar();
}

/**
 * Callbacks interface that all activities using this fragment must implement.
 */
public static interface NavigationDrawerCallbacks {
    /**
     * Called when an item in the navigation drawer is selected.
     */
    void onNavigationDrawerItemSelected(int position);
}
}

解决方案

  1. use android.support.v7.app.ActionBarDrawerToggle instead of android.support.v4.app.ActionBarDrawerToggle

  2. you will get an error for the constructor of ActionBarDrawerToggle. Use the constructor for ActionBarDrawerToggle v7 with 4 parameters.

  3. Also add this to your styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light"> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> </style> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> </style>

4.Apply this theme to your application in the manifest. Hope this helps.

这篇关于更改箭头图标在抽屉菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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