我正在将 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/