Cordova + Android-android-windowSoftInputMode AdjustPan无法正常工作 [英] Cordova + Android - android-windowSoftInputMode adjustPan not working

查看:92
本文介绍了Cordova + Android-android-windowSoftInputMode AdjustPan无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cordova 5.4.0,并且在我的config.xml中有此文件:

I'm using Cordova 5.4.0 and I have this in my config.xml:

<preference name="fullscreen" value="false" />
<preference name="android-windowSoftInputMode" value="adjustPan" />

但是构建后,在我的AndroidManifest.xml中仍然有

but after building, in my AndroidManifest.xml there still is

android:windowSoftInputMode="adjustResize"

为什么它不起作用?而我该如何解决呢?

Why is it not working? And how can I solve it?

推荐答案

android-windowSoftInputMode首选项似乎受

The android-windowSoftInputMode preference seems to be supported by Phonegap only, not Cordova.

解决方法1(Cordova 6.4+):使用修改配置

Workaround 1 (Cordova 6.4+): use edit-config

确保xmlns:android="http://schemas.android.com/apk/res/android"命名空间属性包含在widget元素中,并添加一个edit-config元素:

Make sure the xmlns:android="http://schemas.android.com/apk/res/android" namespace attribute is included in the widget element and add an edit-config element:

<widget xmlns:android="http://schemas.android.com/apk/res/android" ...>
    ...
    <edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:name='MainActivity']" mode="merge">
        <activity android:windowSoftInputMode="adjustPan" />
    </edit-config>
    ...
</widget>

解决方法2(Cordova 6.4之前的版本):使用插件

添加 cordova-custom-config 插件:

cordova plugin add cordova-custom-config

添加以下首选项:

<platform name="android">
    <preference name="android-manifest/application/activity/@android:windowSoftInputMode" value="adjustPan" />
    ...
</platform>

解决方法3:添加一个before_build挂钩

将以下钩子添加到配置.xml:

Add the following hook to config.xml:

<hook type="before_build" src="scripts/appBeforeBuild.js" />

将文件appBeforeBuild.js添加到具有以下内容的脚本目录:

Add a file appBeforeBuild.js to the scripts directory with the following content:

#!/usr/bin/env node

var fs = require('fs');
var path = require('path');

var pathToManifest = path.join(__dirname, '../platforms/android', 'AndroidManifest.xml');
if(fs.existsSync(pathToManifest)) {
    var config = fs.readFileSync(pathToManifest, 'utf8');

    var result = config.replace(/(android:windowSoftInputMode=").*?(")/, '$1adjustPan$2');
    fs.writeFileSync(pathToManifest, result, 'utf8');

    console.log('Set android:windowSoftInputMode to adjustPan');
}
else {
    console.log('Could not find AndroidManifest to set android:windowSoftInputMode');
}

此脚本将使用Node查找AndroidManifest,并对android:windowSoftInputMode属性(仅首次出现)进行正则表达式替换.

This script will use Node to lookup the AndroidManifest and do a regex replace on the android:windowSoftInputMode attribute (first occurrence only).

这篇关于Cordova + Android-android-windowSoftInputMode AdjustPan无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆