最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Width of buttons not changing in react native - Stack Overflow

programmeradmin1浏览0评论

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

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,
},

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

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,
},
Share Improve this question edited May 12, 2020 at 14:07 palaѕн 74k17 gold badges122 silver badges139 bronze badges asked May 12, 2020 at 14:06 cmcodescmcodes 1,87621 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

In React Native you need to wrap it with a View and give a width to the parent.

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 ponent only takes up as much width as the title is, no matter how much you set in the style

Also dont forget to add the 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,
    },
});
发布评论

评论列表(0)

  1. 暂无评论