gpt4 book ai didi

android - 如何从资源创建Drawable

转载 作者:IT老高 更新时间:2023-10-28 12:50:48 27 4
gpt4 key购买 nike

我有一张图片res/drawable/test.png (R.drawable.test)。
我想将此图像传递给接受 Drawable 的函数,例如mButton.setCompoundDrawables().

那么如何将图像资源转换为Drawable

最佳答案

您的 Activity 应该具有 getResources 方法。做:

Drawable myIcon = getResources().getDrawable( R.drawable.icon );


从 API 版本 21 开始,此方法已弃用,可以替换为:

Drawable myIcon = AppCompatResources.getDrawable(context, R.drawable.icon);

如果您需要指定自定义主题,以下将应用它,但前提是 API 版本为 21 或更高版本:

Drawable myIcon =  ResourcesCompat.getDrawable(getResources(), R.drawable.icon, theme);

关于android - 如何从资源创建Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4818118/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com