Android关于Theme.AppCompat相关问题的深入分析(转)
http://www.jianshu.com/p/6ad7864e005e
先来看这样一个错误:
No resource found that matches the given name '@style/Theme.AppCompat.Light'
对于这个错误,相信大部分Android开发者都遇到过,可能很多朋友通过百度或者Google已经解决了这个问题,但是网上大部分都只给出了解决方法。
正所谓知其然,知其所以然,本文将从此问题出发,深入分析探讨导致此问题的原因、由其衍生出来的一系列问题及其解决方案。
Android Support Library
The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs.
Android的SDK版本很多,新的SDK版本包含了很多新的特性,为此Google官方提供Android Support Library package来保证高版本SDK的向下兼容。通过使用此包,可以让拥有最新SDK特性的应用运行在API lever 4(即Android 1.6) 及更高版本的设备之上。
- v4 Support Library
此包用在API lever 4(即Android 1.6)及更高版本之上。它包含了较多的内容,使用非常广泛,例如:Fragment,NotificationCompat,LoadBroadcastManager,ViewPager,PageTabStrip,Loader,FileProvider 等。 v7 Support Libraries
此包是针对API level 7(即Android 2.1)及以上版本而设计的,但是v7是要依赖v4这个包的,v7支持了Action Bar以及一些Theme的兼容。Note: v7 appcompat library
v7 appcompat library 是包含在 v7 Support Libraries里面的一个包,正是此包增加了Action Bar 用户界面的设计模式,并加入了对material design 的支持,是我们使用最多的一个兼容包。v13 Support Library
此包是针对API level 13(即Android 3.2)及更高版本设计的,一般我们都不常用,平板开发中能用到,这里就不过多介绍了。- v17 Preference Support Library for TV
看名字就知道了,此包主要是为了TV设备而设计。
Android Theme
- Hoho Theme
在4.0之前Android可以说是没有设计可言的,在4.0之后推出了Android Design,从此Android在设计上有了很大的改善,而在程序实现上相应的就是Holo风格,所以你看到有类似 Theme.Holo.Light、 Theme.Holo.Light.DarkActionBar 就是4.0的设计风格,但是为了让4.0之前的版本也能有这种风格怎么办呢?这个时候就不得不引用v7包了,所以对应的就有 Theme.AppCompat.Light、Theme.AppCompat.Light.DarkActionBar,如果你的程序最小支持的版本是API14(即Android 4.0),那么可以不用考虑v7的兼容。
- Material Design Theme
Android在5.0版本推出了Material Design的概念,这是Android设计上又一大突破。对应的程序实现上就有Theme.Material.Light、 Theme.Material.Light.DarkActionBar等,但是这种风格只能应用在在5.0版本的手机,如果在5.0之前应用Material Design该怎么办呢?同样的引用appcompat-v7包,这个时候的Theme.AppCompat.Light、Theme.AppCompat.Light.DarkActionBar就是相对应兼容的Material Design的Theme。
问题分析
由此可以得出以下情形会导致本文一开始提出的问题。
- 项目使用的是Theme.AppCompat主题,具体表现为
项目values目录styles.xml文件里面style为<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<style name="AppTheme" parent="AppBaseTheme"></style>
</resources>AndroidManifest.xml文件里面
android:theme="@style/AppTheme"
项目支持的最小SDK小于API 14(即Android4.0),具体表现为
AndroidManifest.xml文件里面,minSdkVersion
<14,比如<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />项目没有导入android-support-v7-appcompat兼容包。
解决方案
此时的解决方法有如下几种:
- 既然没有找到
Theme.AppCompat.Light
主题,那么我就不使用此主题。此时将项目values,values-v11,values-v14目录下的styles.xml文件里面的style都改为<resources>
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<style name="AppTheme" parent="AppBaseTheme"></style>
</resources> 那如果没有找到
Theme.AppCompat.Light
主题,而我们又想要使用最新的主题效果呢,还有种方法就是将AndroidManifest.xml文件里面,minSdkVersion
改成14,比如<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />此时再将项目values,values-v11,values-v14目录下的styles.xml文件里面style都改为
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.Light"></style>
<style name="AppTheme" parent="AppBaseTheme"></style>
</resources>当然以上都不是最好的方法,只是提供一种思路。最好的方法就是导入android-support-v7-appcompat库。下面具体介绍:
3.1 通过Android SDK Manager下载最新的Android Support Library。img1.jpg下载完成之后,可以在以下目录找到AppCompat library
android-sdk/extras/android/support/v7/appcompat
3.2 将此目录下的项目导入到Eclipse中
img2.jpg3.3 右键点击我们的Android项目,选择Properties,左侧选择Android,在下方Library框里点击Add,最后选择appcompat_v7,确定。
此时问题就解决了。img3.jpg但在以上3.2导入appcompat_v7到Eclipse之后,有可能还会出现错误提示,比如
appcompat_v7\res\values-v23\styles_base.xml:20: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.
出现此问题的原因是appcompat_v7已经更新到了最新版本并且高于编译环境的SDK版本,此时在Android SDK Manager将SDK及编译工具更新到最高版本

升级完成之后右键点击appcompat_v7项目,选择Properties,选择Project Build Target 为最新版本,这样就OK了。

通过以上的分析,相信朋友们以后再遇到AppCompat相关的问题应该不再是问题了。欢迎大家留言讨论。
原文链接:http://www.jianshu.com/p/6ad7864e005e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
Android关于Theme.AppCompat相关问题的深入分析(转)的更多相关文章
- 【Android】Theme.AppCompat.Light 问题
Android 开发的 styles.xml 文件中遇到了这个问题: <style name="AppBaseTheme" parent="Theme.AppCom ...
- 2-配置Andriod环境时的错误。。。Theme.AppCompat.Light
编译或运行时可能会出现错误: Error:Error retrieving parent for item: No resource found that matches the given name ...
- 【android】新手容易遇到的[error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.]Theme出错的问题
一.概述 近期刚接手了一个项目,开发工具为eclipse,由于版本较低,且考虑到如果转android studio项目的话,会其他人的维护带来困难,所以想着还是维护项目原来的开发环境吧. 但是导入项目 ...
- android中出现Error retrieving parent for item: No resource found that matches the Theme.AppCompat.Light
styles.xml中<style name="AppBaseTheme" parent="Theme.AppCompat.Light">提示如下错 ...
- Android You need to use a Theme.AppCompat theme (or descendant) with this activity.
错误描述为:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...
- 创建Android项目时出错——No resource found that matches the given name 'Theme.AppCompat.Light'
创建Android项目时出错,error: Error retrieving parent for item: No resource found that matches the given nam ...
- Android Studio:You need to use a Theme.AppCompat theme (or descendant) with this activity. AlertDialog
学习<第一行代码>的时候遇到的问题. Process: com.example.sevenun.littledemo, PID: 2085 java.lang.RuntimeExcepti ...
- Android Studio: You need to use a Theme.AppCompat theme (or descendant) with this activity.
错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...
- 项目引入android-support-v7-appcompat遇到的问题,no resource found that matches the given name 'android:Theme.AppCompat.Light'
一.问题 今天准备使用v7包中的ToolBar来用,但是在styles.xml中引入Theme.AppCompat.Light的时候,报错“no resource found that matches ...
随机推荐
- js 函数提升和变量提升
总结: 函数提升比变量提升优先级高! 词法分析 词法分析方法: js运行前有一个类似编译的过程即词法分析,词法分析主要有三个步骤: 分析参数 再分析变量的声明 分析函数说明 具体步骤如下: 函数在运行 ...
- 3-MSP430引脚中断
为了写一篇文章做铺垫--提醒着自己,,,,,, 这两天一直在寻找 #pragma vector = PORT1_VECTOR __interrupt void P1_Interrupt()//P1口中 ...
- atitit.提升备份文件复制速度(3) ----建立同步删除脚本
atitit.提升备份文件复制速度(3) ----建立同步删除脚本 1. 建立同步删除脚本两个方法.. 1 2. 1从回收站info2文件... 1 3. 清理结束在后snap比较 1 4. Npp ...
- UVa 10387- Billiard
UVa 10387- Billiard Table of Contents 1 题目 2 思路 3 代码 4 参考 1 题目 ============= Problem A: Billiard In ...
- 493萬Gmail用戶的賬號密碼遭洩露,Google否認自己存在安全漏洞
最近,大公司在互聯網信息安全問題上狀況頻出.上週,蘋果因iCloud被黑客攻擊而導致大量明星私照外洩,著實是熱鬧了一陣.而Google也來湊熱鬧了.據俄羅斯媒體CNews消息,近493萬Gmail用戶 ...
- RecyclerView的使用
什么是RecyclerView RecyclerView是Android 5.0 materials design中的组件之一,相应的还有CardView.Palette等.看名字我们 ...
- ZooKeeper典型应用场景一览
原文地址:http://jm-blog.aliapp.com/?p=1232 ZooKeeper典型应用场景一览 数据发布与订阅(配置中心) 发布与订阅模型,即所谓的配置中心,顾名思义就是发布者将数据 ...
- A little tutorial on CodeFluent Entities with ASP.NET MVC4
/* Author: Jiangong SUN */ CodeFluent Entities is a model-first development tool which creates non-s ...
- Maven Learning - Direct Dependencies & Transitive Dependencies
Dependencies declared in your project's pom.xml file often have their own dependencies. The main dep ...
- 常用天气预报API接口整理(转)
文章转自:http://www.nohacks.cn/post-35.html 自序: 由nohacks.cn 收集整理,来源于网络,版权归原作者所有,基本收集了网络上能使用的大部分天气API接口,作 ...