反应本机布局行为不当

时间:2019-08-09 02:33:55

标签: react-native ignite-ui react-native-flexbox

我正在尝试使用Ignite学习本机React。一直在争取布局。 这是我的主要容器渲染功能:

render () {
return (
  <View style={styles.mainContainer}>
    <Image source={Images.background} style={styles.backgroundImage} resizeMode='stretch' />
    <View style={[styles.container]}>
      <View style={styles.section} >
        {/* <Image source={Images.ready} />*/}
        <Text style={styles.sectionText}>
          Tap to randomly choose your training task. Slack off for 5
        </Text>
      </View>

      <View style={styles.centered}>
        <TouchableOpacity onPress={this._onPressButton}>
          <Image source={Images.launch} style={styles.logo} />
        </TouchableOpacity>
      </View>
    </View>
    <View style={[styles.bottom]}>
      <View >
        <BottomBar />
      </View>
    </View>
  </View>
)
}

特别是,容器的最后一个兄弟具有一个带有BottomBar组件的视图.bottom样式执行以下操作:

bottom: {
      justifyContent: 'flex-end',
      marginBottom: Metrics.baseMargin
  }

BottomBar组件:

import React, { Component } from 'react'
// import PropTypes from 'prop-types';
import { View, Text, TouchableOpacity } from 'react-native'
import styles from './Styles/BottomBarStyle'

import Icon from 'react-native-vector-icons/FontAwesome'

export default class BottomBar extends Component {
  // // Prop type warnings
  // static propTypes = {
  //   someProperty: PropTypes.object,
  //   someSetting: PropTypes.bool.isRequired,
  // }
  //
  // // Defaults for props
  // static defaultProps = {
  //   someSetting: false
  // }

  render () {
    console.tron.log('rendering my component')
    console.tron.log('Bottom bar styles: \n',styles)
    return (
      <View style={[styles.iconsContainer, styles.debugGreen]}>
        <TouchableOpacity style={[styles.icons,styles.debugYellow]} onPress={()=>{console.tron.log('rocket')}} >
          <Icon style={styles.icons} name='rocket' size={40} color='white' />
        </TouchableOpacity>
        <TouchableOpacity style={styles.button} onPress={ ()=>{console.tron.log('send')} }>
          <Icon style={styles.icons} name='send' size={40} color='white' />
        </TouchableOpacity>
      </View>
    )
  }
}

与之关联的样式:

import { StyleSheet } from 'react-native'
import DebugStyles from '../../Themes/DebugStyles'
import { Metrics } from '../../Themes/'



export default StyleSheet.create({
  ...DebugStyles,
  iconsContainer: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    height: 45,
    borderRadius: 5,
    marginHorizontal: Metrics.section,
    marginVertical: Metrics.baseMargin
  },
  icons:{
    height: 45

    }

})

我遇到的问题是,如果我看到Rounded按钮的bottomBar组件是这样的:

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { TouchableOpacity, Text } from 'react-native'
import styles from './Styles/RoundedButtonStyles'
import ExamplesRegistry from '../Services/ExamplesRegistry'

// Note that this file (App/Components/RoundedButton) needs to be
// imported in your app somewhere, otherwise your component won't be
// compiled and added to the examples dev screen.

// Ignore in coverage report
/* istanbul ignore next */
ExamplesRegistry.addComponentExample('Rounded Button', () =>
  <RoundedButton
    text='real buttons have curves'
    onPress={() => window.alert('Rounded Button Pressed!')}
  />
)
console.tron.log('Rounded button style: ',styles)
export default class RoundedButton extends Component {
  static propTypes = {
    onPress: PropTypes.func,
    text: PropTypes.string,
    children: PropTypes.string,
    navigator: PropTypes.object
  }

  getText () {
    const buttonText = this.props.text || this.props.children || ''
    return buttonText.toUpperCase()
  }

  render () {
    console.tron.log('roundedButton styles:', styles)
    return (
      <TouchableOpacity style={styles.button} onPress={this.props.onPress}>
        <Text style={styles.buttonText}>{this.getText()}</Text>
      </TouchableOpacity>
    )
  }
}

及其样式:

import { StyleSheet } from 'react-native'
    import { Fonts, Colors, Metrics } from '../../Themes/'

    export default StyleSheet.create({
      button: {
        height: 45,
        borderRadius: 5,
        marginHorizontal: Metrics.section,
        marginVertical: Metrics.baseMargin,
        backgroundColor: Colors.fire,
        justifyContent: 'center'
      },
      buttonText: {
        color: Colors.snow,
        textAlign: 'center',
        fontWeight: 'bold',
        fontSize: Fonts.size.medium,
        marginVertical: Metrics.baseMargin
      }
    })

我得到了预期的视图:

expected positioning

但是,通过我的BottomBar组件,我得到:

Shifted down

要注意的一件事是,debugGreen样式只是一个边框,应该环绕在我的BottomBar组件周围,并且显示为平坦,但是其中的图标呈较低状态,并且图标周围的debugYellow样式框显示在图标周围正如预期的那样,只是整个向下移动了。

1 个答案:

答案 0 :(得分:1)

如果mainContainer的视图是flex : 1height : 100%,则应将孩子的height除以8:2或将flex除以8:2

示例

<View style={styles.mainContainer}> // flex: 1
  <View style={styles.container}> // flex : 0.8
...
  </View>
    <View style={styles.bottom}> // flex : 0.2
        <BottomBar />
    </View>
</View>