将文件添加到 /res/values?
Add file to /res/values?
是否可以指定要添加到 Android /res/values 文件夹中的文件?
我正在尝试添加自定义主题来更改本机输入选择器的外观,例如 <input type="date">
。我找到了 cordova-custom-config 插件,可以让我在 AndroidManifest.xml 中设置主题,但它对实际添加文件没有任何作用
想通了:
我在 config.xml
中为 android 平台添加了一个挂钩
<hook type="before_prepare" src="scripts/020_copy_resources.js" />
钩子里,我运行一个基于this answer的脚本,保存在<project>/scripts/020_copy_resources.js"
#!/usr/bin/env node
// Native resources to copy
var androidNativePath = 'native/android/';
// Android platform resource path
var androidResPath = 'platforms/android/res/';
// Copy native resources
var rootdir = process.argv[2];
var fs = require('fs');
var path = require('path');
function copyFileSync( source, target ) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
process.stdout.write('Copying ' + source + ' to ' + targetFile);
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync( source, target ) {
var files = [];
//check if folder needs to be created or integrated
//var targetFolder = path.join( target, path.basename( source ) );
var targetFolder = target;
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
process.stdout.write('fs.mkdirSync( ' + targetFolder + ' )');
}
//copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
var newTarget = path.join( targetFolder, path.basename( curSource ) );
copyFolderRecursiveSync( curSource, newTarget );
process.stdout.write('copyFolderRecursiveSync(' + curSource + ', ' + newTarget + ' )');
} else {
copyFileSync( curSource, targetFolder );
process.stdout.write('copyFileSync(' + curSource + ', ' + targetFolder + ' )');
}
} );
}
}
if (rootdir) {
copyFolderRecursiveSync(androidNativePath, androidResPath);
process.stdout.write('Copied android native resources');
}
这会将 <project>/native/android
中的任何 files/folders 复制到 <project>/platforms/android/res
然后我按照 为日期选择器构建主题。出于我的目的,我只需要在主题中设置 colorAccent
和 colorBackground
以及 datePickerDialogTheme
.
是否可以指定要添加到 Android /res/values 文件夹中的文件?
我正在尝试添加自定义主题来更改本机输入选择器的外观,例如 <input type="date">
。我找到了 cordova-custom-config 插件,可以让我在 AndroidManifest.xml 中设置主题,但它对实际添加文件没有任何作用
想通了:
我在 config.xml
中为 android 平台添加了一个挂钩<hook type="before_prepare" src="scripts/020_copy_resources.js" />
钩子里,我运行一个基于this answer的脚本,保存在<project>/scripts/020_copy_resources.js"
#!/usr/bin/env node
// Native resources to copy
var androidNativePath = 'native/android/';
// Android platform resource path
var androidResPath = 'platforms/android/res/';
// Copy native resources
var rootdir = process.argv[2];
var fs = require('fs');
var path = require('path');
function copyFileSync( source, target ) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
process.stdout.write('Copying ' + source + ' to ' + targetFile);
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync( source, target ) {
var files = [];
//check if folder needs to be created or integrated
//var targetFolder = path.join( target, path.basename( source ) );
var targetFolder = target;
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
process.stdout.write('fs.mkdirSync( ' + targetFolder + ' )');
}
//copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
var newTarget = path.join( targetFolder, path.basename( curSource ) );
copyFolderRecursiveSync( curSource, newTarget );
process.stdout.write('copyFolderRecursiveSync(' + curSource + ', ' + newTarget + ' )');
} else {
copyFileSync( curSource, targetFolder );
process.stdout.write('copyFileSync(' + curSource + ', ' + targetFolder + ' )');
}
} );
}
}
if (rootdir) {
copyFolderRecursiveSync(androidNativePath, androidResPath);
process.stdout.write('Copied android native resources');
}
这会将 <project>/native/android
中的任何 files/folders 复制到 <project>/platforms/android/res
然后我按照 colorAccent
和 colorBackground
以及 datePickerDialogTheme
.