RemoteViews的layout无法设置xml类型的Drawable,会报错

这里写自定义目录标题

例如这样的:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <corners android:radius="10dp" />
            <size
                android:width="48dp"
                android:height="48dp" />
            <solid android:color="@color/color_red" />
        </shape>
    </item>

    <item
        android:bottom="6dp"
        android:left="6dp"
        android:right="6dp"
        android:top="6dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/lingxi_ic_hang_up" />
    </item>
</layer-list>

可在代码里这样使用:

java 复制代码
convertToBitmap(R.drawable.bg_lingxi_hang_up_view)?.let { setImageViewBitmap(R.id.image_hang_up, it)}
java 复制代码
    private fun convertToBitmap(@DrawableRes drawableRes: Int): Bitmap? {
        try {
            // 将 Drawable 转换为 Bitmap
            val drawable = TelApplication.instance?.let { ContextCompat.getDrawable(it, drawableRes) }
            AppLog.info(TAG, "convertToBitmap drawable: $drawable")
            if (drawable != null) {
                val bitmap = Bitmap.createBitmap(
                    drawable.intrinsicWidth,
                    drawable.intrinsicHeight,
                    Bitmap.Config.ARGB_8888
                )
                val canvas = Canvas(bitmap)
                drawable.setBounds(0, 0, canvas.width, canvas.height)
                drawable.draw(canvas)
                AppLog.info(TAG, "convertToBitmap bitmap: $bitmap")
                return bitmap
            }
        } catch (e: Exception) {
            e.printStackTrace()
            AppLog.error(TAG, "convertToBitmap", e)
        }

        AppLog.info(TAG, "convertToBitmap null")
        return null
    }
相关推荐
web守墓人2 天前
【前端】ikun-pptx编辑器前瞻问题五:pptx中的xml命名空间
xml·前端
h7ml2 天前
企业微信回调模式解析:从XML到POJO的自定义JAXB编解码器设计
xml·java·企业微信
Full Stack Developme2 天前
达梦(DM8)对 JSON 与 XML 的使用教程
xml·数据库·json
chilavert3183 天前
技术演进中的开发沉思-304计算机原理:XML
xml·计算机原理
程序猿零零漆4 天前
Spring之旅 - 记录学习 Spring 框架的过程和经验(十一)基于XML方式、注解的声明式事务控制、Spring整合Web环境
xml·学习·spring
科雷软件测试5 天前
推荐几个常用的校验yaml、json、xml、md等多种文件格式的在线网站
xml·html·md·yaml
susu10830189115 天前
maven-3.9.12的conf配置settings.xml
xml·java·maven
odoo中国7 天前
如何在 Odoo 中从 XML 文件调用函数
xml·odoo·odoo开发·调用函数
阿凉07028 天前
新版本JLink安装目录中缺失JLinkDevices.xml添加方法
xml·嵌入式硬件
Knight_AL8 天前
从 QueryWrapper 到 XML:一次「报表 SQL」的重构实践
xml·sql·重构