如何使用Appcompat v7 21,工具栏和DrawerLayout将汉堡带到箭头

我正在使用android.support.v4.widget.DrawerLayout的android.support.v7.widget.Toolbar。 它工作正常,导航抽屉closures时显示汉堡图标,抽屉打开时显示箭头图标。 我想禁用抽屉,并在应用程序的某些事件中将汉堡图标animation成箭头。 我试图设置locking模式closures,但v7.app.ActionBarDrawerToggle仍然显示汉堡,它打开抽屉。

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

有任何想法吗? 谢谢!

更新:

不,我可以改变图标的​​状态,我可以启用/禁用抽屉,但animation不能用这种方法:

@Override protected void onCreate(Bundle savedInstanceState) { ... Toolbar toolbar = (Toolbar) findViewById(R.id.application_toolbar); setSupportActionBar(toolbar); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.string1, R.string.string2) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } }; //mDrawerLayout.setDrawerListener(mDrawerToggle); // not needed ... } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: if (mDrawerLayout.getDrawerLockMode(GravityCompat.START) == LOCK_MODE_UNLOCKED) { showDrawer(); } else { handleBackButtonPress(); // On this stage the home button is a <- } } ... } private void setDrawerState(boolean isEnabled) { if (isEnabled) { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerToggle.syncState(); } else { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); mDrawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); mDrawerToggle.syncState(); } } 

抽屉出现在工具栏的顶部。

Solutions Collecting From Web of "如何使用Appcompat v7 21,工具栏和DrawerLayout将汉堡带到箭头"

看看这里,它描述了你如何解决它。

https://stackoverflow.com/a/26447144

基本部分如下:

 <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> <item name="color">@android:color/white</item> </style> 

我创build了一个具有类似function的小应用程序

主要活动

 public class MyActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer); android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.open, R.string.close ) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); invalidateOptionsMenu(); syncState(); } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); syncState(); } }; drawerLayout.setDrawerListener(actionBarDrawerToggle); //Set the custom toolbar if (toolbar != null){ setSupportActionBar(toolbar); } getSupportActionBar().setDisplayHomeAsUpEnabled(true); actionBarDrawerToggle.syncState(); } } 

我的XML活动

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MyActivity" android:id="@+id/drawer" > <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/toolbar_custom"/> </FrameLayout> <!-- The navigation drawer --> <ListView android:layout_marginTop="?attr/actionBarSize" android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#457C50"/> </android.support.v4.widget.DrawerLayout> 

我的自定义工具栏XML

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbar" android:background="?attr/colorPrimaryDark"> <TextView android:text="U titel" android:textAppearance="@android:style/TextAppearance.Theme" android:textColor="@android:color/white" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.Toolbar> 

我的主题风格

 <resources> <style name="AppTheme" parent="Base.Theme.AppCompat"/> <style name="AppTheme.Base" parent="Theme.AppCompat"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primaryDarker</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> </style> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> <item name="color">@android:color/white</item> </style> <color name="primary">#457C50</color> <color name="primaryDarker">#580C0C</color> </resources> 

我的风格值-V21

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppTheme" parent="AppTheme.Base"> <item name="android:windowContentTransitions">true</item> <item name="android:windowAllowEnterTransitionOverlap">true</item> <item name="android:windowAllowReturnTransitionOverlap">true</item> <item name="android:windowSharedElementEnterTransition">@android:transition/move</item> <item name="android:windowSharedElementExitTransition">@android:transition/move</item> </style> </resources>