【问题标题】:Add file to /res/values?将文件添加到 /res/values?
【发布时间】:2017-08-01 04:29:51
【问题描述】:

是否可以指定要添加到 Android /res/values 文件夹中的文件?

我正在尝试添加自定义主题来更改本机输入选择器的外观,例如<input type="date">。我找到了 cordova-custom-config 插件,它可以让我在 AndroidManifest.xml 中设置主题,但这对于实际添加文件没有任何作用

【问题讨论】:

    标签: android phonegap-cli


    【解决方案1】:

    想通了:

    我在config.xml中为android平台添加了一个钩子

    <hook type="before_prepare" src="scripts/020_copy_resources.js" />
    

    在钩子中,我运行了一个基于this answer的脚本,保存在&lt;project&gt;/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');
    }
    

    这会将&lt;project&gt;/native/android 中的所有文件/文件夹复制到&lt;project&gt;/platforms/android/res

    然后我跟随this answer 为日期选择器构建主题。就我而言,我只需要在主题中设置colorAccentcolorBackground 以及datePickerDialogTheme

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多