android - KeyboardAvoidingView 中的高度 vs 位置 vs 填充 "behavior"

标签 android ios react-native

KeyboardAvoidingView 中有一个“行为”属性,例如:

import { KeyboardAvoidingView } from 'react-native';

<KeyboardAvoidingView style={styles.container} behavior="padding" enabled>
    ... your UI ...
</KeyboardAvoidingView>

它可以被选为以下三个选项之一:'height''position''padding'。区别不解释in the documentation .它只是说不需要设置属性,并且有一个注释:

Note: Android and iOS both interact with this prop differently. Android may behave better when given no behavior prop at all, whereas iOS is the opposite.

这些设置应该有什么效果?

最佳答案

我同意这里缺少文档令人沮丧。如果你深入研究 KeyboardAvoidingView 的源代码, 你会在 behavior 周围找到一个开关: https://github.com/facebook/react-native/blob/92073d4a71d50a1ed80cf9cb063a6144fcc8cf19/Libraries/Components/Keyboard/KeyboardAvoidingView.js#L157

看起来发生了以下情况:

height

A <View>返回的样式试图为 View 设置静态高度,该高度可以是屏幕高度,也可以是屏幕高度减去键盘(当键盘存在时)。

position

嵌套 <View>返回,其中外部 View 应用了您的样式,内部 View 具有 bottom应用的样式试图在键盘存在时匹配键盘高度。

padding

<View>paddingBottom 处返回如果键盘存在,样式将应用于键盘的高度。


鉴于可用的任意选项,使用 KeyboardAvoidingView 时看起来像您应该反复试验,并检查两种设备以获得您想要的结果。理论上所有三个选项都应该有效,但正如文档所说,设备类型之间存在一些细微差别。

在我看来,这个组件应该被废弃,取而代之的是随着时间的推移返回键盘高度的辅助函数,这样您就可以直接根据键盘的可见性应用您自己的风格想法。

关于android - KeyboardAvoidingView 中的高度 vs 位置 vs 填充 "behavior",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47661480/

相关文章:

javascript - '安卓 :name' in <activity> tag must be a valid Java class name in React Native

ios - 如何在 swift 更改(Firebase)时接收/更新数据

android - java.lang.NoClassDefFoundError : Failed resolution of: Lcom/google/android/gms/common/internal/zzbq; 错误

react-native - 在 native react 中获取 "Cannot read property ' pickAlgorithm' of null”错误

android - Abi 过滤器 - 被一个或多个具有更高版本代码的 APK 遮蔽

android - 如何使用终端命令对 Android 应用程序进行签名

android - 如何在 Android Volley/Picasso/Glide 中更快地下载图像?

ios - 在 UIGestureRecognizerState 不工作的情况下点击手势

java - Flutter如何解析JSON Mongodb数据

javascript - React Native ECMAScript 6 对象语法只有键,没有显式值?