我遵循这个tutorial(github code)的唯一区别是,由于ActionBarActivity显示为已弃用,我扩展Activity并使用getActionBar()来获取对它的引用。我认为其余的东西与教程一样,也是导入。
除了一件事,它对我有用。我总是在MainActivity中看到后面的箭头图标(仅在其中)。我不想在我的操作栏中显示后退箭头,我只想让ic_drawer或hamburguer图标通知用户有选项。所以我不介意如果我不能在箭头和显示教程的hamburguer图标之间获得动画。我只想在那里显示hamburguer图标,但现在只显示箭头图标。
我尝试了几个this post的解决方案,但它们都没有为我工作。
我的styles.xml如下所示:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<!-- navigation Drawer's -->
<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
</style>
</resources>
注意:我的ic_drawer图标位于drawable- * dpi文件夹内,而不在drawable文件夹中。
也按建议使用syncState:
@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);
}
在我的build.gradle中:
minSdkVersion 17
targetSdkVersion 22
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
}
解决方案:我将styles.xml更改为:
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo">
<!-- navigation Drawer's -->
<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>
</style>
此外,我意识到我忘了在Manifest中包含我正在使用的风格:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="false">