需要定义一个半透明的activity,在manifest中添加 android:theme="@android:style/Theme.Translucent"
结果报错"You need to use a Theme.AppCompat theme (or descendant) with this activity." 由于我的主题设置为
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">,猜想是不是这个原因造成的(没有去验证),网上的很多方法是设置直接
继承 Activity。
我查了下Theme.Translucent的源码,为:
<style name="Theme.Translucent">
<item name="windowBackground">@color/transparent</item>
<item name="colorBackgroundCacheHint">@null</item>
<item name="windowIsTranslucent">true</item>
<!-- Note that we use the base animation style here (that is no
animations) because we really have no idea how this kind of
activity will be used. -->
<item name="windowAnimationStyle">@style/Animation</item>
</style>
根据源码我在style.xml中自定了<style name="Theme.AppCompat.Translucent">,将源码中的item复制过去:
<style name="Theme.AppCompat.Translucent">
<item name="windowBackground">@color/transparent</item>
<item name="colorBackgroundCacheHint">@null</item>
<item name="windowIsTranslucent">true</item>
<item name="windowAnimationStyle">@style/Animation</item>
</style>
结果报错:No resource found that matches the given name: attr 'colorBackgroundCacheHint'.
No resource found that matches the given name: attr 'windowAnimationStyle'.
No resource found that matches the given name: attr 'windowBackground'.
No resource found that matches the given name: attr 'windowIsTranslucent'.
后来发现复制的<item name="windowBackground">@color/transparent</item>如果用快捷键去关联生成的是:<item name="android:windowBackground">@android:color/transparent</item>,最后修改为:
<style name="Theme.AppCompat.Translucent">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
程序运行通过。通过这个说明其他的定义主题出现的类似问题,都可以这样解决。