如何在Cordova插件

时间:2018-01-02 05:39:51

标签: android cordova cordova-plugins

我将Android原生代码转换为Cordova插件。在本机我使用以下代码在浮动操作按钮

中设置图像
    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原生中的可绘制文件夹)

1 个答案:

答案 0 :(得分:2)

来自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);

使用plugin.xml中的resource-file tag

复制图像
<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" />