最近遇到一个bug,android8.0的手机,半透明的activity如果设置了旋转,就会发生崩溃。但是为了允许平板横屏,手机竖屏,又必须设置旋转方向,所以就需要在代码中获取当前主题,并判断它是不是透明的主题,代码如下

    public static boolean isTranslucentActivity(Activity activity){
        int themeId = getThemeResId(activity);
        if (themeId == android.R.style.Theme_Translucent || themeId == android.R.style.Theme_Translucent_NoTitleBar || themeId == android.R.style.Theme_Translucent_NoTitleBar_Fullscreen ){
            return true;
        }
        return false;
    }

    private static int getThemeResId(Activity activity) {
        try {
            ActivityInfo activityInfo = activity.getPackageManager().getActivityInfo(
                    activity.getComponentName(), PackageManager.GET_META_DATA);
            return activityInfo.theme;
        } catch (PackageManager.NameNotFoundException e) {
            LogUtils.e(TAG, "get activity theme failed: " + e.toString());
            return 0;
        }
    }

我项目中只使用了三种透明主题,可根据自己项目实际情况修改。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐