gpt4 book ai didi

javascript - 在 React Native 中使 View 为父级的 80% 宽度

转载 作者:IT王子 更新时间:2023-10-29 02:48:26 28 4
gpt4 key购买 nike

我正在 React Native 中创建一个表单,并想让我的 TextInput 占屏幕宽度的 80%。

使用 HTML 和普通 CSS,这会很简单:

input {
display: block;
width: 80%;
margin: auto;
}

除了 React Native 不支持 display 属性、百分比宽度或自动边距。

那么我应该怎么做呢?有 some discussion of this problem在 React Native 的问题跟踪器中,但建议的解决方案看起来像是讨厌的黑客。

最佳答案

从 React Native 0.42 开始,height:width: 接受百分比。

在样式表中使用 width: 80% 就可以了。

  • 截图

  • 实例
    <强> Child Width/Height as Proportion of Parent

  • 代码

    import React from 'react';
    import { Text, View, StyleSheet } from 'react-native';

    const width_proportion = '80%';
    const height_proportion = '40%';

    const styles = StyleSheet.create({
    screen: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#5A9BD4',
    },
    box: {
    width: width_proportion,
    height: height_proportion,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#B8D2EC',
    },
    text: {
    fontSize: 18,
    },
    });

    export default () => (
    <View style={styles.screen}>
    <View style={styles.box}>
    <Text style={styles.text}>
    {width_proportion} of width{'\n'}
    {height_proportion} of height
    </Text>
    </View>
    </View>
    );

关于javascript - 在 React Native 中使 View 为父级的 80% 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33939974/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com