You need to enable JavaScript to run this app. Android 8.0(API 26)下弹出式缩放Activity背景缓慢变黑问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Android 8.0(API 26)下弹出式缩放Activity背景缓慢变黑问题求助

问题原因

该问题是Android 8.0(API 26)的系统渲染特性导致的:API 26对半透明悬浮Activity的绘制逻辑做了特殊调整,同时配置android:windowIsFloating=true和android:windowIsTranslucent=true两个属性时,会触发SurfaceFlinger服务的alpha值计算异常,窗口重绘时透明背景的alpha值会被错误累加,最终慢慢变成全黑。通过getWindow().setLayout()动态调整窗口大小的操作会触发多次窗口重绘,进一步加重这个异常。

解决方案

1. 分版本配置主题,移除属性冲突

不要在API 26上同时开启悬浮和半透明属性,针对不同系统版本做适配:

  • 通用配置(res/values/styles.xml)
<style name="PopupTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>
  • API 26专属配置(res/values-v26/styles.xml)
<style name="PopupTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>
  • API 27及以上配置(res/values-v27/styles.xml),可保留原有配置
<style name="PopupTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

2. 调整窗口大小的调用时机

不要在onCreate生命周期直接调用setLayout,改为等待View绘制完成后再调整,避免触发不必要的重绘:

getWindow().getDecorView().post(() -> {
    WindowManager.LayoutParams params = getWindow().getAttributes();
    // 替换为你自己的宽高逻辑
    params.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.8);
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    getWindow().setAttributes(params);
});

3. 可选:禁用系统默认转场动画

如果修改主题后仍有异常,可在Activity的onCreate中关闭默认转场动画,避免动画渲染和窗口重绘冲突:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(0, 0);
    // 其余原有逻辑
}

内容的提问来源于stack exchange,提问作者Cactusroot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 13:24:03