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
    }
相关推荐
小短腿的代码世界31 分钟前
Qt SVG渲染管线全解析:从XML解析到像素绘制的完整架构设计与性能优化实战
xml·qt·性能优化
HMS工业网络21 小时前
技术干货:EtherCAT设备ESI(XML)文件中的CompleteAccess关键字有什么作用
xml·运维·服务器
鹏晨互联2 天前
【Compose vs XML:边框内外间距的实现对比】
android·xml
鹏晨互联3 天前
Jetpack Compose vs XML:fillMaxSize、fillMaxHeight、fillMaxWidth 全面对比
android·xml
如果'\'真能转义说4 天前
OOXML 文档格式剖析:哈希、ZIP结构与识别
xml·算法·c#·哈希算法
ZC跨境爬虫5 天前
跟着 MDN 学 HTML day_34:(深入XML 中的 CDATASection 接口)
xml·前端·html·html5·媒体
hmywillstronger5 天前
【Python】从SAP2000 XML截面库提取数据到Excel
xml·python·excel
Boop_wu5 天前
[Mybatis] XML 方式实现 MP 自定义 SQL + 条件构造器
xml·sql·mybatis
曹牧6 天前
Oracle:将包含属性(Attributes)的 XML 数据解析为表格数据
xml·数据库·oracle