Toolbar Back Icon

#1.问题

setSupportActionBar(toolbar);
if (activity.getSupportActionBar() != null){
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

Error : Cannot resolve symbol

abc_ic_ab_back_mtrl_am_alpha

#2.原因

com.android.support:appcompat-v7:24.0.0里的资源文件

abc_ic_ab_back_mtrl_am_alpha

名称被修改为

abc_ic_ab_back_material

VectorDrawable文件。

#3.解决方案

使用23.1.1修改

R.drawable.abc_ic_ab_back_mtrl_am_alpha

使用24.0.0修改

R.drawable.abc_ic_ab_back_material

R.drawable.abc_ic_ab_back_material的内容为:

<?xml version="1.0" encoding="utf-8"?>
<!--
   Copyright (C) 2015 The Android Open Source Project

   Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:autoMirrored="true"
        android:tint="?attr/colorControlNormal">
    <path
            android:pathData="M20,11L7.8,11l5.6,-5.6L12,4l-8,8l8,8l1.4,-1.4L7.8,13L20,13L20,11z"
            android:fillColor="@android:color/white"/>
</vector>

API小于21需要在gradle文件中添加以下配置:

  • Gradle Plugin 2.0+

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }
  • Gradle Plugin 1.5

android {  
       defaultConfig {  
         generatedDensities = []  
      }  

      // This is handled for you by the 2.0+ Gradle Plugin  
      aaptOptions {  
        additionalParameters "--no-version-vectors"  
      }  
 }
I Don't Want Your Money, I Want Aragaki Yui.