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
    }
相关推荐
那个失眠的夜28 分钟前
Spring 的纯注解配置
xml·java·数据库·后端·spring·junit
mobai74 小时前
使用pyang将yang模型转换为xml
xml·运维·服务器
我不是8神15 小时前
xml配置文件知识点总结
xml
一叶龙洲2 天前
Java中使用模板引擎(FreeMarker / Velocity) + Word XML导出复杂Word
xml·java·word
Mike_6663 天前
txt_json和xml_json
xml·python·json
20YC编程社区3 天前
一分钟了解XML语言格式,使用场景,和它的优缺点
xml
Kay.Wen3 天前
LabVIEW 转换 XML文本
xml·labview
练习时长一年3 天前
浅谈assembly插件打包分发机制
xml
历程里程碑5 天前
Protobuf vs JSON vs XML:小白该怎么选?
xml·大数据·数据结构·elasticsearch·链表·搜索引擎·json
那个失眠的夜6 天前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis