简体   繁体   English

按钮的宽度在本机反应中没有改变

[英]Width of buttons not changing in react native

I have two buttons in a row separated by the padding of 20 and they have occupied the width needed for the title of the button.我连续有两个按钮,由 20 的填充隔开,它们占据了按钮标题所需的宽度。 Their width doesn't change!它们的宽度不会改变!

Code for buttons按钮代码

<View style={styles.buttonContainer}>

  <View style={{ paddingLeft: 20 }}>
    <Button title={tick} style={styles.button} onPress color="tomato" />
  </View>

  <View style={{ paddingLeft: 20 }}>
    <Button title="X" style={styles.button} onPress color="tomato" />
  </View>

</View>

CSS for buttons CSS 用于按钮

buttonContainer: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'flex-end'
},
button: {
    width: 50, // no matter what value I set here, their width doesn't changes
    height: 20,
},

In React Native you need to wrap it with a View and give a width to the parent.在 React Native 中,您需要用View包装它并为父级指定宽度。

So in your example you should put:因此,在您的示例中,您应该输入:

<View style={{ paddingLeft: 20, width: 50, height: 20 }}>
    <Button title="X" style={styles.button} color="tomato" />
</View>

Make sure you wrap the button around with an extra View.确保使用额外的视图包裹按钮。 The Button component only takes up as much width as the title is, no matter how much you set in the style Button 组件只占用与标题一样多的宽度,无论您在样式中设置多少

Also dont forget to add the onPress function, boolean (true) is an invalid prop也不要忘记添加 onPress function, boolean (true) is an invalid prop

export default function App() {
    const tick = 'tick';
    return (
        <View style={styles.buttonContainer}>
            <View style={{ paddingLeft: 20 }}>
                <Button title={tick} style={styles.button} onPress color="tomato" />
            </View>

            <View style={styles.buttonStyle}>
                <Button title="X" style={styles.button} onPress color="tomato" />
            </View>
        </View>
    );
}

const styles = StyleSheet.create({
    buttonContainer: {
        flex: 1,
        flexDirection: 'row',
        justifyContent: 'flex-end',
    },
    buttonStyle: {
        paddingLeft: 20,
        width: 200, // this way it works
    },
    button: {
        height: 20,
    },
});

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

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