android - 如何在 Cordova 插件中设置图像

标签 android cordova cordova-plugins

我正在将 android native 代码转换为 Cordova 插件。在 native 中,我使用以下代码在 float 操作按钮中设置图像

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.icon_record, getTheme()));
    } else {
        floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.icon_record));
    } 

我的疑问是,在 android 中我们将使用 R.drawable.XXXXXX 来设置图像。同样明智的是我们如何在 cordova 插件中设置图像以及我们必须在哪里存储图像(就像 android native 中的可绘制文件夹)

最佳答案

示例来自 InAppBrowser插件

Resources activityRes = cordova.getActivity().getResources();
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
Drawable backIcon = activityRes.getDrawable(backResId);
if (Build.VERSION.SDK_INT >= 16)
    back.setBackground(null);
else
    back.setBackgroundDrawable(null);
back.setImageDrawable(backIcon);

图像是使用 resource-file tag 复制的在 plugin.xml 中

<resource-file src="src/android/res/drawable-hdpi/ic_action_previous_item.png" target="res/drawable-hdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-mdpi/ic_action_previous_item.png" target="res/drawable-mdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-xhdpi/ic_action_previous_item.png" target="res/drawable-xhdpi/ic_action_previous_item.png" />
<resource-file src="src/android/res/drawable-xxhdpi/ic_action_previous_item.png" target="res/drawable-xxhdpi/ic_action_previous_item.png" />

关于android - 如何在 Cordova 插件中设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48056056/

相关文章:

android - 如何在 Androidx Compose Material 中删除 TextField 的指示线?

javascript - XHR 进度事件在上传完成之前不会触发?

javascript - 即使在添加控制台插件后,console.log 在 iOS Phonegap App 中也不起作用

ionic-framework - resolveLocalFileSystemURI : Expected String, 的参数 "uri"的类型错误,但得到 Null

java - okhttp3 请求android项目失败

java - 如何在 ImageView 中只显示某个位图?

android - 将 SeparatedListAdapter 用于 ListView 在 Android 2.1 上崩溃但适用于 2.2

javascript - Phonegap - 如何从索引页面调用另一个文件中的一个 javascript 函数?

android - Phonegap Contacts API - Android - 只返回电话联系人而不返回 gmail 联系人

cordova - 在浏览器中模拟 Cordova 插件