在React Native中放置相对于其他视图的视图

时间:2017-06-27 11:33:45

标签: react-native flexbox

我尝试创建这样的东西: enter image description here

如您所见,文本应相对于代表当前日期的标记(位于上方居中)放置。当你在前几天和最后几天,它应该留在框架内,而不是在高标记上方居中。

我如何实现这一目标?

这是我目前的代码:

组件

 export default class DateLine extends Component {
  render() {
    const dots = (<View style={Styles.dotsContainer} >
      {[...Array(DaysInCurrentMonth())].map((x, i) =>
        <View key={i.toString()} style={[Styles.dot, (CurrentDayInMonth() === (i + 1) ? Styles.tallDot : null), (CurrentDayInMonth() < (i + 1) ? Styles.grayDot : null)]} />
      )}
    </View>);
    return (
      <View style={Styles.container}>
        <Text style={Styles.text}>{GetDateWithMonthAsText(Date()).toUpperCase()}</Text>
        {dots}
      </View>
    );
  }
}

风格:

export default StyleSheet.create({
  container: {
    alignSelf: 'stretch',
    marginHorizontal: 15
  },

  dotsContainer: {
    height: 10,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'flex-end'
  },

  dot: {
    backgroundColor: Colors.darkBlueGrey,
    height: 5,
    width: 5,
    borderRadius: 2.5
  },

  tallDot: {
    height: 10
  },

  grayDot: {
    backgroundColor: Colors.pinkishGreyTwo
  },

  text: {
    fontWeight: '600'
  }
});

1 个答案:

答案 0 :(得分:0)

不是一个完美但快速的解决方案,您可以轻松改进:

export default class DateLine extends Component {
    _renderDay (x, i) {
        let currentDay = CurrentDayInMonth();
        if(currentDay > (i + 1)) {
            return <PassedDay />;
        }

        if(currentDay === (i + 1)) {
            return <ActiveDate dateTitle={ GetDateWithMonthAsText(Date()).toUpperCase() } />;
        }

        return <InactiveDay />;
    }

    render() {
        return (
            <View style={ Styles.container }>
              <View style={ Styles.dotsContainer } >
                {[...Array(DaysInCurrentMonth())].map(this._renderDay)}
            </View>
          </View>
        );
    }
}

export function PassedDay() {
    return (<View>
        <View style={{backgroundColor: '#1f1749', width: 5, height: 5, borderRadius: 5, marginHorizontal: 2.5,}} />
    </View>);
}

export function ActiveDate(dateTitle) {
    return (<View style={{position: 'relative'}}>
        <View style={{position: 'absolute', minWidth: 70, bottom: 15, left: 0, right: 0,}}>
            <Text>
                {dateTitle}
            </Text>
        </View>
        <View style={{backgroundColor: '#1f1749', width: 5, height: 10, borderRadius: 5, marginHorizontal: 2.5,}} />
    </View>);
}

export function InactiveDay() {
    return (<View>
        <View style={{backgroundColor: 'gray', width: 5, height: 5, borderRadius: 5, marginHorizontal: 2.5,}} />
    </View>);
}

关键是:你可以通过react-native的位置以与html相同的方式操纵你的'DOM'。

希望,这会有所帮助。