Change android theme from cordova config.xml

前端 未结 4 1665
南笙
南笙 2020-12-17 03:18

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

相关标签:
4条回答
  • 2020-12-17 03:29

    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."

    for example:

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

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

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

    0 讨论(0)
  • 2020-12-17 03:41

    You can use Header Color plugin:

    Install the plugin:

    $ ionic cordova plugin add cordova-plugin-headercolor $ npm install --save @ionic-native/header-color

    Add configuration to config.xml <preference name="HeaderColor" value="#becb29" />

    https://ionicframework.com/docs/native/header-color/

    0 讨论(0)
  • 2020-12-17 03:45

    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.

    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);
    }
    
    0 讨论(0)
  • 2020-12-17 03:46

    You can do this now without any third party plugin since 6.3.0. Just add this to the config.xml

    <platform name="android">
        <edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:label='@string/activity_name']" mode="merge">
            <activity android:theme="@android:style/Theme.Translucent"></activity>
        </edit-config>
    </platform>
    

    and for me it was also neccessary to add 'xmlns:android="http://schemas.android.com/apk/res/android" ' to the widget tag

    <widget id="de.bestellkind.restaurant" version="1.0.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    
    0 讨论(0)
提交回复
热议问题