简体   繁体   English

从cordova config.xml更改android主题

[英]Change android theme from cordova config.xml

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?

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 plugin add cordova-custom-config --save

  2. Config.xml: <preference name="android-manifest/application/activity/@android:theme" value="@android:style/Theme.Holo"/> 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 . 这会将属性“android:theme”添加到您的AndroidManfiset - > application - > activity,其值为: @android:style/Theme.Holo

To avoid touching the platforms directory, you could use a cordova hook . 为避免触及平台目录,您可以使用cordova 挂钩 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. 首先在npm install elementtree然后在hooks文件夹中创建一个子文件夹after_prepare From there stick this code into a javascript file and change YourTheme. 从那里将此代码粘贴到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);
}

You can do this now without any third party plugin since 6.3.0. 从6.3.0开始,您可以在没有任何第三方插件的情况下执行此操作。 Just add this to the config.xml 只需将其添加到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 对我来说,还需要将'xmlns:android =“http://schemas.android.com/apk/res/android”添加到小部件标签中

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

You can use Header Color plugin: 您可以使用Header Color插件:

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" /> 将配置添加到config.xml <preference name="HeaderColor" value="#becb29" />

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM