简体   繁体   English

如何在 React Native 的同一行中放置两个按钮?

[英]How to place two buttons within the same row in react native?

Within a popup dialog I want to buttons to be placed on the same line but now I'm getting like this https://i.stack.imgur.com/sJ30p.png.Following is the style given.在弹出对话框中,我想将按钮放置在同一行,但现在我得到这样的https://i.stack.imgur.com/sJ30p.png.Following是给定的样式。

    <PopupDialog
                    ref={popupDialog => {
                      this.popupDialog = popupDialog;
                    }}
                    dialogStyle={{ backgroundColor: "#ddd", height: 200, width:320, border:10,padding:10}}
                    overlayBackgroundColor="#fff"
                    dismissOnTouchOutside={true}
                         >
                 <View style={styles.dialogContentView}>
                 <Text style={{fontSize:18}}>Are you sure you want to submit?</Text>
                 <View style={styles.button_1}>
                 <Button
    title="Yes"
    onPress={() => {
    console.log('clicked')
    }}
    />
    </View>
    <View style={styles.button_1}>
    <Button
    title="No"
    onPress={() => {
    console.log('clicked')
    }}
    />
    </View>
                </View>
                  </PopupDialog>

.....
....

dialogContentView: {
  flex: 1,
    flexDirection: 'column',
    justifyContent: 'space-between'
},
button_1:{

      width: '40%',
      height: 30,

} }

Add View parent to both Button component with style flexDirection: 'row' after </Text>View父级添加到两个Button组件,样式为flexDirection: 'row' after </Text>

<View style={{ flexDirection: 'row' }}>
  <View style={styles.button_1}>
    <Button
      title="Yes"
      onPress={() => {
        console.log('clicked');
      }}
   />
   </View>
   <View style={styles.button_1}>
     <Button
       title="No"
       onPress={() => {
         console.log('clicked');
       }}
     />
   </View>
 </View>

You can try this snack你可以试试这个小吃

Try this for full width without any spaces or margin JUST COPY PASTE试试这个没有任何空格边距的全宽只需复制粘贴

 <View style={{ flexDirection: "row" }}>
     <View style={{ flex: 1 }}>
         <TouchableOpacity style={{ alignSelf: 'stretch',backgroundColor: '#2980B9' }} onPress={() => { onSavePress }}>
              <Text style={{ alignSelf: 'center',
                            color: '#ffffff',
                            fontSize: 16,
                            fontWeight: '600',
                            paddingTop: 10,
                            paddingBottom: 10 }}>Save</Text>
         </TouchableOpacity>
     </View>
     <View style={{borderLeftWidth: 1,borderLeftColor: 'white'}}/>
     <View style={{ flex: 1}}>
         <TouchableOpacity style={{ alignSelf: 'stretch',backgroundColor: '#2980B9'}} onPress={() => { onCancelPress }}>
              <Text style={{ alignSelf: 'center',
                            color: '#ffffff',
                            fontSize: 16,
                            fontWeight: '600',
                            paddingTop: 10,
                            paddingBottom: 10 }}>Cancel</Text>
         </TouchableOpacity>
     </View>
 </View>

To do that, I usually create a view, set its flexDirection to 'row' and put the button components inside of it.为此,我通常会创建一个视图,将其flexDirection设置为'row'并将按钮组件放入其中。 So, your code would look like this:因此,您的代码将如下所示:

<View style={{ flexDirection: 'row' }}>
 <Button title='Button 1'/>
 <Button title='Button 2'/>
</View>

I hope it helps.我希望它有帮助。

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

相关问题 如何在react native中设置同一行的两个输入? - How to set Two inputs on same row in react native ? 如何正确放置同一行中的两个单选按钮或复选框? - How to properly distance two radio buttons or checkboxes in the same row? 如何在同一个屏幕中更改视图(React Native)? - How to change views within the same screen (React Native)? 两个提交按钮在同一个表单中 - two submit buttons within the same form React Native - 如何使用相同的代码在 react-native 的两个不同页面中获得相同的输出? - React Native - How to get same output in two different pages in react-native with same code? 在ScrollView中使用react-native-collapsible呈现每行两个项目 - render two items per row with react-native-collapsible within ScrollView React Native如何在同一个动画事件中传递两个事件? - React Native how to pass two events in the same animated event? 如何在两个不同的.js文件中使用相同的数据(React Native) - How to use same data in two different .js file (React Native) 如何在同一组件中使用两个 React Native 日期选择器模式 - How To Use Two React Native date Picker modal in same component React Native-在同一组件中修改道具 - React Native - Modify a prop within the same component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM