从cordova config.xml更改android主题

时间:2021-01-26 07:27:12

At my company we are using Ionic Framework and Cordova to create our mobile app, and upon starting the design of the app, we encountered an issue with the Android theme and how to set it without touching the AndroidManifest generated by Ionic build command.

在我的公司,我们使用Ionic Framework和Cordova来创建我们的移动应用程序,在开始设计应用程序时,我们遇到了Android主题的问题以及如何在不触及Ionic build命令生成的AndroidManifest的情况下设置它。

Everywhere I look it is recommended to implement any customization from the config.xml file and never touch the AndroidManifest, but I cant seem to find any methods regarding the Android theme.

无论我到哪里,建议从config.xml文件实现任何自定义,永远不要触摸AndroidManifest,但我似乎无法找到任何关于Android主题的方法。

My question to you now: Is there a way to set the android theme for the application, for example Holo Theme, from the Config.xml without changing the AndroidManifest.xml generated?

我现在问你的问题:有没有办法在不更改AndroidManifest.xml生成的情况下从Config.xml为应用程序设置android主题,例如Holo Theme?

2 个解决方案

#1


4  

To avoid touching the platforms directory, you could use a cordova hook. I am pretty terrible at node, but here is something that should do the trick. First npm install elementtree then create a sub folder after_prepare in the hooks folder. From there stick this code into a javascript file and change YourTheme.

为避免触摸平台目录,您可以使用cordova挂钩。我在节点上非常糟糕,但这里应该可以做到这一点。首先在npm安装elementtree然后在hooks文件夹中创建一个子文件夹after_prepare。从那里将此代码粘贴到javascript文件中并更改YourTheme。

Honestly, this is some pretty gross code, but should give you the idea.

老实说,这是一些相当粗略的代码,但应该给你一个想法。

#!/usr/bin/env node
var fs = require( "fs" );
var et = require('elementtree');
var rootdir = process.argv[2];
console.log(rootdir);
fs.open(rootdir + '/platforms/android/AndroidManifest.xml', 'r+',
    function (err, fd)  {
        if (err) {
            exitError(err);
        }
        fs.stat(rootdir + '/platforms/android/AndroidManifest.xml', getStats);

        function getStats(error, stats) {
            if (error) {
                exitError(error);
            }
            var buffer = new Buffer(stats.size);
            fs.read(fd, buffer, 0, buffer.length, null, fileRead);
        }

        function fileRead(error, bytes, buf) {
            var data = buf.toString("utf8", 0, buf.length);
            var androidXML = et.parse(data);
            var root = androidXML.getroot();
            var activityTag = root.find("application/activity");
            activityTag.attrib["android:theme"] = "@style/YourTheme";
            var outputBuffer = new Buffer(et.tostring(root), "utf-8");
            console.log(outputBuffer.toString());
            fs.closeSync(fd);
            fs.open(rootdir + '/platforms/android/AndroidManifest.xml', 'w', writeFile);
            function writeFile(error, fd) {
                if (error) {
                    exitError(error);
                }
                fs.write(fd, outputBuffer, 0, outputBuffer.length, 0, function( errw, written, str) {
                    if (errw) {
                        exitError(errw);
                    }
                    console.log('file written');
                    fs.close(fd);
                });
            }

        }
    });

function exitError(error) {
    console.log(error);
    process.exit(0);
}

#2


5  

I know I'm late, but cordova-custom-config plugin made just to "update platform configuration files based on preferences and config-file data defined in config.xml."

我知道我迟到了,但是cordova-custom-config插件只是为了“根据config.xml中定义的首选项和配置文件数据更新平台配置文件”。

for example:

  1. install the cordova-custom-config plugin: cordova plugin add cordova-custom-config --save

    安装cordova-custom-config插件:cordova插件添加cordova-custom-config --save

  2. Config.xml: <preference name="android-manifest/application/activity/@android:theme" value="@android:style/Theme.Holo"/>

    Config.xml:

This will add the attribute "android:theme" to your AndroidManfiset --> application --> activity with the value: @android:style/Theme.Holo.

这会将属性“android:theme”添加到您的AndroidManfiset - > application - > activity,其值为:@android:style / Theme.Holo。

#1


4  

To avoid touching the platforms directory, you could use a cordova hook. I am pretty terrible at node, but here is something that should do the trick. First npm install elementtree then create a sub folder after_prepare in the hooks folder. From there stick this code into a javascript file and change YourTheme.

为避免触摸平台目录,您可以使用cordova挂钩。我在节点上非常糟糕,但这里应该可以做到这一点。首先在npm安装elementtree然后在hooks文件夹中创建一个子文件夹after_prepare。从那里将此代码粘贴到javascript文件中并更改YourTheme。

Honestly, this is some pretty gross code, but should give you the idea.

老实说,这是一些相当粗略的代码,但应该给你一个想法。

#!/usr/bin/env node
var fs = require( "fs" );
var et = require('elementtree');
var rootdir = process.argv[2];
console.log(rootdir);
fs.open(rootdir + '/platforms/android/AndroidManifest.xml', 'r+',
    function (err, fd)  {
        if (err) {
            exitError(err);
        }
        fs.stat(rootdir + '/platforms/android/AndroidManifest.xml', getStats);

        function getStats(error, stats) {
            if (error) {
                exitError(error);
            }
            var buffer = new Buffer(stats.size);
            fs.read(fd, buffer, 0, buffer.length, null, fileRead);
        }

        function fileRead(error, bytes, buf) {
            var data = buf.toString("utf8", 0, buf.length);
            var androidXML = et.parse(data);
            var root = androidXML.getroot();
            var activityTag = root.find("application/activity");
            activityTag.attrib["android:theme"] = "@style/YourTheme";
            var outputBuffer = new Buffer(et.tostring(root), "utf-8");
            console.log(outputBuffer.toString());
            fs.closeSync(fd);
            fs.open(rootdir + '/platforms/android/AndroidManifest.xml', 'w', writeFile);
            function writeFile(error, fd) {
                if (error) {
                    exitError(error);
                }
                fs.write(fd, outputBuffer, 0, outputBuffer.length, 0, function( errw, written, str) {
                    if (errw) {
                        exitError(errw);
                    }
                    console.log('file written');
                    fs.close(fd);
                });
            }

        }
    });

function exitError(error) {
    console.log(error);
    process.exit(0);
}

#2


5  

I know I'm late, but cordova-custom-config plugin made just to "update platform configuration files based on preferences and config-file data defined in config.xml."

我知道我迟到了,但是cordova-custom-config插件只是为了“根据config.xml中定义的首选项和配置文件数据更新平台配置文件”。

for example:

  1. install the cordova-custom-config plugin: cordova plugin add cordova-custom-config --save

    安装cordova-custom-config插件:cordova插件添加cordova-custom-config --save

  2. Config.xml: <preference name="android-manifest/application/activity/@android:theme" value="@android:style/Theme.Holo"/>

    Config.xml:

This will add the attribute "android:theme" to your AndroidManfiset --> application --> activity with the value: @android:style/Theme.Holo.

这会将属性“android:theme”添加到您的AndroidManfiset - > application - > activity,其值为:@android:style / Theme.Holo。