Android--You need to use a Theme.AppCompat theme (or descendant) with this activity解决方案

来源:互联网 发布:淘宝产品拍摄价格表 编辑:程序博客网 时间:2024/05/17 01:30

一:错误场景
自定义一个Activity的theme;而这个Activity继承自v7包中的AppCompatActivity或者ActionBarActivity:在Activity中指定相应的theme:

android:theme="@style/Transparent"

配置相应的styles.xml:

<style name="Transparent" parent="AppTheme.Base">    <item name="android:windowBackground">@color/transparent</item>    <item name="android:windowIsTranslucent">true</item>    <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item></style>

二:代码错误

You need to use a Theme.AppCompat theme (or descendant) with this activity.

三:解决方案:
1 --:继承自Activity即可
----:无法兼容老版本样式
----:无法实现5.0的MaterialDesign效果

2 --:
--1:在res/values/styles.xml:
添加一个样式为AppTheme.Base,然后Transparent继承自AppTheme.Base

<style name="Transparent" parent="AppTheme.Base">    // 此处写自己的代码</style><style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">    <item name="colorPrimary">@color/colorPrimary</item>    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    <item name="colorAccent">@color/colorAccent</item>    <item name="android:windowBackground">@android:color/white</item></style>

--2:新建values-v21文件夹,新建styles.xml:
新建样式,名字为Transparent并且继承自AppTheme.Base:

<style name="Transparent" parent="AppTheme.Base">    <item name="android:colorPrimary">@color/colorPrimary</item>    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>    <item name="android:colorAccent">@color/colorAccent</item></style>

说明:values-v21文件夹中的内容是专门针对API21以上的版本所使用的配置文件,也就是说如果是API21之前的文件就是使用res/values中的styles.xml,否则使用values-v21文件夹下的styles.xml。

0 0