【问题标题】:react native flexbox split screen反应原生 flexbox 分屏
【发布时间】:2016-04-19 19:16:39
【问题描述】:

我正在尝试使用 flexbox 拆分屏幕,但我没有得到我想要的结果,这就是我所拥有的

<View style={{flex: 1}}>
    <View style={{flex: 1}}>{/* half of the screen */}</View>

    <View style={{flex: 1}}>{/* the other half */}
        {/*<Swiper>*/}
            <View style={{flex: 1}}>{/* a quarter of the other half */}</View>
            <View style={{flex: 1}}>{/* a quarter of the other half */}</View>
            <View style={{flex: 1}}>{/* a quarter of the other half */}</View>
            <View style={{flex: 1}}>{/* a quarter of the other half */}</View>
        {/*</Swiper>*/}
    </View>
</View>

我遇到的问题是屏幕的另一半扩展为全屏的大小,它只是附加到前半部分而没有考虑到它存在于其中的一半的范围

screenshot

我应该如何处理这个问题?

【问题讨论】:

    标签: react-native flexbox


    【解决方案1】:

    嗯,经过一年多的学习,我现在有点知道自己在 React Native 中做什么了。

    <View style={{ flex: 1 }}>
      <View style={{ backgroundColor: 'gray', flex: 1 }} />
    
      <View style={{ flex: 1 }}>
        <Swiper>
          <View
            style={{
              alignItems: 'center',
              backgroundColor: 'red',
              justifyContent: 'center',
              height: Dimensions.get('window').height / 2
            }}>
            <Text style={{ color: 'white' }}>Test</Text>
          </View>
          <View
            style={{
              alignItems: 'center',
              backgroundColor: 'green',
              justifyContent: 'center',
              height: Dimensions.get('window').height / 2
            }}>
            <Text style={{ color: 'white' }}>Test</Text>
          </View>
          <View
            style={{
              alignItems: 'center',
              backgroundColor: 'blue',
              justifyContent: 'center',
              height: Dimensions.get('window').height / 2
            }}>
            <Text style={{ color: 'white' }}>Test</Text>
          </View>
        </Swiper>
      </View>
    </View>
    

    【讨论】:

    【解决方案2】:

    尝试将flexDirection: 'row' 样式赋予最外层视图。

    【讨论】:

    • 这只改变了我不想要的方向,请注意我发布的屏幕截图中,我在其中一个四分之一处添加了一些文本并将其放在中心,它被修剪了,因为某些原因它遵循父级的父级限制而不是父级iteslf
    • 如果你把示例代码放到 rnplay.org,我去看看。
    • 我正在为四个四边形使用 react-native-swiper 包装器,它无法在 rnplay 上运行
    【解决方案3】:

    这是我的解决方案:https://rnplay.org/apps/DQ2gxA

    基本思想是将列的数量(在本例中为 8)作为常数,然后根据需要拆分可用空间。并将flexDirection: 'row' 与父容器一起使用。

    【讨论】:

    • 我试过你的方法,由于某种原因它对我的代码不起作用,我正在为四个四边形使用 react-native-swiper 包装器,本质上,三个四边形总是隐藏的,只有一个正在显示,这有什么影响吗?它不应该!看看文本在其中一个四边形中的位置,即使它已正确弯曲,它也不在中间i.imgur.com/IAU9tdL.png
    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2018-04-29
    • 2017-12-08
    • 2019-05-15
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多