更改默认抽屉图标(汉堡包)

时间:2016-05-23 18:36:45

标签: android android-fragments navigation-drawer android-toolbar

我想更改打开抽屉的默认图标。有没有办法可以做到?我搜索了一下,我无法解决它。这是我的导航抽屉类,我在其他需要片段的类中实例化它。

if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){

但是,当我将此代码public class navigation_drawer extends Fragment { public static final String PREF_FILE_NAME = "testpref"; public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer"; private ActionBarDrawerToggle drawer_toggle; private DrawerLayout my_drawer_layout; private View container_view; private boolean m_userLearnedDrawer; private boolean m_fromSavedInstanceState; public navigation_drawer() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); m_userLearnedDrawer = Boolean.valueOf( readFromPreferences( getActivity() , KEY_USER_LEARNED_DRAWER , "false" ) ); if( savedInstanceState != null ) { m_fromSavedInstanceState = true; } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_navigation_drawer, container, false); } public void setUp( int fragmentId , DrawerLayout dl , final Toolbar toolbar ) { container_view = getActivity().findViewById( fragmentId ); my_drawer_layout = dl; drawer_toggle = new ActionBarDrawerToggle( getActivity() , dl , toolbar , R.string.drawer_open , R.string.drawer_close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if( ! m_userLearnedDrawer ) { m_userLearnedDrawer = true; saveToPreferences( getActivity() , PREF_FILE_NAME , m_userLearnedDrawer + "" ); } getActivity().invalidateOptionsMenu(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getActivity().invalidateOptionsMenu(); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item !=null && item.getItemId() == android.R.id.home) { if (my_drawer_layout.isDrawerOpen(Gravity.RIGHT)) { my_drawer_layout.closeDrawer(Gravity.RIGHT); } else { my_drawer_layout.openDrawer(Gravity.RIGHT); } return true; } return false; } @Override public void onDrawerSlide(View drawerView, float slideOffset) { if( slideOffset < 0.1 ) { toolbar.setAlpha(1 - slideOffset); } } }; /* if( !m_userLearnedDrawer && !m_fromSavedInstanceState ) { my_drawer_layout.openDrawer( container_view ); } */ my_drawer_layout.setDrawerListener( drawer_toggle ); my_drawer_layout.post( new Runnable() { @Override public void run() { drawer_toggle.syncState(); } } ); } public static void saveToPreferences( Context con , String preferenceName , String preferenceValue) { SharedPreferences sp = con.getSharedPreferences( PREF_FILE_NAME , Context.MODE_PRIVATE ); SharedPreferences.Editor editor = sp.edit(); editor.putString( preferenceName , preferenceValue ); editor.apply(); } public static String readFromPreferences( Context con , String preferenceName , String preferenceValue) { SharedPreferences sp = con.getSharedPreferences( PREF_FILE_NAME , Context.MODE_PRIVATE ); return sp.getString( preferenceName , preferenceValue ); } } 放在onDrawerOpened中时,它会显示默认的汉堡图标,但是当我滑动或单击图标时,它会更改为我的图片并替换默认图标。 这是我实现导航抽屉类的类。

toolbar.setNavigationIcon(R.drawable.picture);

1 个答案:

答案 0 :(得分:0)

你需要在切换后调用syncState()后调用它,所以在Runnable的run()方法之后将其移到后面。