css - React Native 中的响应式网格

标签 css react-native flexbox

我的应用主页由一个导航栏和六个大圆形按钮组成。
我想将按钮放置在纵向 2x3 和横向 3x2 的(响应式)网格中。
像这样的东西,视觉上:

portrait:
+-----------+
|   navbar  |
|           |
| +--+ +--+ |
| |B1| |B2| |
| +--+ +--+ |
| +--+ +--+ |
| |B3| |B4| |
| +--+ +--+ |
| +--+ +--+ |
| |B5| |B6| |
| +--+ +--+ |
+-----------+

landscape:
+-------------------------+
|         navbar          |
|   +--+   +--+   +--+    |
|   |B1|   |B2|   |B3|    |
|   +--+   +--+   +--+    |
|   +--+   +--+   +--+    |
|   |B4|   |B5|   |B6|    |
|   +--+   +--+   +--+    |
+-------------------------+

我目前的解决方案并不令人满意,因为它在改变方向时没有正确调整大小:

在肖像上(很好):
enter image description here

但是,在风景上:
enter image description here

这是我的组件渲染方法:
const rows = 3;
const cols = 2;
const marginHorizontal = 4;
const marginVertical = 4;
const width = (Dimensions.get('window').width / cols) - (marginHorizontal * (cols + 1));
const height = (Dimensions.get('window').height / rows) - (marginVertical * (rows + 1));

render() {
  return (
  <Container ref="root">
    <ScrollView style={stylesGrid.scrollContainer}>
      <View style={stylesGrid.sectionContainer}>
        <View style={stylesGrid.boxContainer}><Text>B1</Text></View>
        <View style={stylesGrid.boxContainer}><Text>B2</Text></View>
        <View style={stylesGrid.boxContainer}><Text>B3</Text></View>
        <View style={stylesGrid.boxContainer}><Text>B4</Text></View>
        <View style={stylesGrid.boxContainer}><Text>B5</Text></View>
        <View style={stylesGrid.boxContainer}><Text>B6</Text></View>
      </View>
    </ScrollView>
  </Container>
);
}

const stylesGrid = StyleSheet.create({
  scrollContainer: {
    flex: 1,
  },
  sectionContainer: {
    flex: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'center',
    alignItems: 'center',
  },
  boxContainer: {
    marginTop: marginVertical,
    marginBottom: marginVertical,
    marginLeft: marginHorizontal,
    marginRight: marginHorizontal,
    width: width,
    height: height,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'gold',
  },
});

在横向加载时会发生更糟糕的事情:更改方向时甚至不保留 3x2 布局......

在 react-native 中是否有一致的响应式布局的建议?

最佳答案

我解决了以下@agmcleod 建议:在构造函数中添加了一个方向更改监听器:

Dimensions.addEventListener('change', () => {
  if (this.refs.root) { // avoid updating state when not necessary
    this.setState({
      orientation: Dimensions.get('window').width < Dimensions.get('window').height ? 'portrait' : 'landscape'
    });
  }
});

然后移动用于在 render() 函数中设置布局变量的逻辑。

奇迹般有效。

关于css - React Native 中的响应式网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569794/

相关文章:

javascript - Angular 2 FlexLayout - 使用断点更改页面内容

jquery - 如何更改 :-moz-placeholder color with jQuery once it's set?

css - Bootstrap 3 - 列的划分

CSS 过滤器灰度在 Firefox 中不起作用

html - IE8 中两个固定 div 元素之间的响应 div

html - 为可选 flex 元素分配空间

android - 使用 React Native 录制 Espresso 测试

javascript - FlatList 仅在至少有 2 个项目时可见

javascript - React-Native:如何创建多个 Native UI 组件实例?

css - 两个 div 并排,等高由一个控制