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
    }
相关推荐
hashiqimiya13 小时前
每日android布局xml文件
android·xml·gitee
2601_9611940217 小时前
27考研资料|百度网盘|夸克网盘
android·xml·考研·ios·iphone·xcode·webview
许彰午4 天前
在PowerBuilder里手写XML序列化——没有现成库的年代怎么拼报文
xml·linux·服务器
坚果的博客5 天前
鸿蒙PC三方库适配OAT.xml 与 SHA512SUM 解读:开源合规与源码校验
xml·开源·harmonyos
奇树谦5 天前
YAML、XML、JSON、TOML、INI、CSV 全面对比:配置文件和数据交换到底该怎么选?
xml·json
南山丶无梅落6 天前
XXE漏洞
xml·漏洞·xxe·网安
小书房6 天前
Android UI为什么由XML转向Compose
xml·ui·compose·声明式ui
学编程的小程6 天前
配置范式演进:XML、JavaConfig 与 Spring Boot
xml·spring boot·后端
le1616167 天前
Android Compose基础布局——从传统XML的视角切入了解
xml·compose
XiYang-DING8 天前
【MyBatis】XML方式实现CRUD
xml·mybatis