react-native - 如何在 React Native 中设置边距

标签 react-native

我正在尝试在 React Native 中设置框的边距,但行为很奇怪。如果我设置 marginLeft: 10,则左边距为 10。但是如果我也设置了 marginRight: 10,那么左边的 margin 是 20,右边的是 20。这是我的代码:

// ResultList.js
const box = {
  backgroundColor: 'green',
  marginLeft: 10,
  marginRight: 10
}
return (
  <View style={box}>
    <ResultItem />
    <ResultItem />
    <ResultItem />
    <ResultItem />
    <ResultItem />
  </View>
) 

// ResultItem.js
return (
  <View style={{margin: 0}}>
    <Text style={{margin: 0}}>
      This is text Here. This is more text testing. 
      This is text Here. This is more text testing
    </Text>
  </View>
)

此代码生成以下图像。我画黑条的地方是绿色框应该对齐的地方。值得注意的是,如果我删除 marginRight: 10,那么左边距的行为是正确的。只有当我在 marginRight 中添加时才错。

enter image description here

蓝色背景 View 的代码:

App.js
return (
  <View style={styles.container}> 
    <ResultList />
  </View>
);

container: {
  flex: 1,
  alignItems: 'center',
  backgroundColor: 'blue'
}

最佳答案

尝试使用 ma​​rginHorizo​​ntal 而不是 marginLeft 和 marginRight。

const box = {
  backgroundColor: 'green',
  marginHorizontal:10
}

关于react-native - 如何在 React Native 中设置边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55700714/

相关文章:

reactjs - React Native JS 调试/热重载在物理设备上不起作用

react-native - React Native - 使用 StyleSheet 与普通对象相比有什么好处?

android - 如何禁止在 expo/react-native 中使用平板电脑?

ios - signInWithPhoneNumber() 在 react-native 中不起作用

reactjs - POST 方法在 react native 应用程序 (Android) 中不起作用(Fetch 或 Axios)

css - React Native 中的样式化组件未按预期工作 - 需要帮助以基于父容器创建可自动调整大小的图像

javascript - 在 React Native 中重新渲染 - 问题

javascript - 每个模块只有一个默认导出允许使用 Navbar 在 React Native 上出错

javascript - RNFetchBlob - 无法在生成的图像文件中获取 API 数据

javascript - React Native map 渲染在一切之上