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

需要定义一个半透明的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>
程序运行通过。通过这个说明其他的定义主题出现的类似问题,都可以这样解决。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值