无法在Android 5.0以下的设备上展开工具栏

3

我正在尝试在Android 5.0以下的设备上扩展toolbar。我使用的主题是基于Theme.AppCompat.Light的,因此像?attr/actionBarSize这样的属性应该可以正常工作。

但是,我遇到了以下错误:

 Error inflating class android.support.v7.widget.Toolbar
 Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v19/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020016

我从XML中得到的工具栏如下:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/material_drawer_primary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/****"
        app:popupTheme="@style/****"
        app:contentInsetStart="72dp"/>

我的主题是这样的:

<style name="****" parent="Theme.AppCompat.Light">
        <!-- ...and here we setting appcompat’s color theming attrs -->
        <item name="colorPrimary">@color/material_drawer_primary</item>
        <item name="colorPrimaryDark">@color/material_drawer_primary_dark</item>
        <item name="colorAccent">@color/material_drawer_accent</item>

        <!-- MaterialDrawer specific values -->
        <item name="material_drawer_background">@color/material_drawer_background</item>
        <item name="material_drawer_primary_text">@color/material_drawer_primary_text</item>
        <item name="material_drawer_primary_icon">@color/material_drawer_primary_icon</item>
        <item name="material_drawer_secondary_text">@color/material_drawer_secondary_text</item>
        <item name="material_drawer_hint_text">@color/material_drawer_hint_text</item>
        <item name="material_drawer_divider">@color/material_drawer_divider</item>
        <item name="material_drawer_selected">@color/material_drawer_selected</item>
        <item name="material_drawer_selected_text">@color/material_drawer_selected_text</item>
        <item name="material_drawer_header_selection_text">@color/material_drawer_header_selection_text</item>
    </style>

有没有办法解决这个问题?谢谢! 编辑1:我也尝试过使用Theme.AppCompat.Light.NoActionBar
2个回答

9

找到了答案: https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88

将Android支持库更新至23.2.0会导致错误:XmlPullParserException Binary XML file line #17<vector> tag requires viewportWidth > 0

似乎更新到支持库23.2.0会导致此问题。

对于那些不想深入研究的人,你只需要做以下几步:

如果你使用的是Gradle版本2.0或以上:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

或者,如果您的版本是1.5或更低版本:

android {
  defaultConfig {
    // Stops the Gradle plugin’s automatic rasterization of vectors
    generatedDensities = []
  }
  // Flag to tell aapt to keep the attribute ids around
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

4

对我来说,这解决了问题。

http://blog.autsoft.hu/do-this-when-upgrading-to-support-library-23-2/

为了防止链接被删除或移除,这里包括必要的代码。

根据您使用的 Gradle 版本,对于新的 2.0 Gradle 插件,只需要一行代码:

// Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

对于Gradle版本1.5(旧版本的Gradle没有此修复程序):

// Gradle Plugin 1.5  
 android {  
   defaultConfig {  
     generatedDensities = []  
  }  

  // This is handled for you by the 2.0+ Gradle Plugin  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
 } 

如果在你的应用程序的build.gradle文件中设置了这些选项,即使在旧设备上,一切都将恢复正常。此外,请不要忘记升级Gradle插件到1.5.0或更新版本,因为旧版本不受支持。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接