3

我想让红色视图保持 16:9 的比例。我尝试但失败了。我知道 React Native 使用 Flexbox(在 Javascript 中重新实现),但我不知道该怎么做。谢谢。

这是我的Javascript:

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  View,
} = React;

var AwesomeProject = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <View style={styles.banner}>

        </View>
        <View style={styles.items}>

        </View>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  banner: {
    backgroundColor: 'red',
    flex: 1,
  },
  items: {
    backgroundColor: 'blue',
    flex: 3,
  },
});

AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);

这是 React Native 中有关 Flexbox 的文档:

http://facebook.github.io/react-native/docs/flexbox.html#content

这是有效的样式道具:

Valid style props: [
  "width",
  "height",
  "top",
  "left",
  "right",
  "bottom",
  "margin",
  "marginVertical",
  "marginHorizontal",
  "marginTop",
  "marginBottom",
  "marginLeft",
  "marginRight",
  "borderWidth",
  "borderTopWidth",
  "borderRightWidth",
  "borderBottomWidth",
  "borderLeftWidth",
  "position",
  "flexDirection",
  "flexWrap",
  "justifyContent",
  "alignItems",
  "alignSelf",
  "flex",
  "resizeMode",
  "backgroundColor",
  "borderColor",
  "borderRadius",
  "tintColor",
  "opacity",
  "fontFamily",
  "fontSize",
  "fontWeight",
  "fontStyle",
  "lineHeight",
  "color",
  "containerBackgroundColor",
  "textAlign",
  "writingDirection",
  "padding",
  "paddingVertical",
  "paddingHorizontal",
  "paddingTop",
  "paddingBottom",
  "paddingLeft",
  "paddingRight",
  "borderTopColor",
  "borderRightColor",
  "borderBottomColor",
  "borderLeftColor",
  "overflow",
  "shadowColor",
  "shadowOffset",
  "shadowOpacity",
  "shadowRadius",
  "transformMatrix",
  "rotation",
  "scaleX",
  "scaleY",
  "translateX",
  "translateY"
]"
4

2 回答 2

7

React Native(从 0.40 开始)支持aspectRatioprop。

你可以做:

style={{ aspectRatio: 16/9 }}

请参阅在 React Native 中保持全宽图像的纵横比

于 2015-04-05T04:18:35.343 回答
0

您可以使用布局功能。

    class AwesomeProject = extends React.Component<{}> {

    constructor(props) {
            super(props)
            this.state = {width: 0,height:0}
        }

        onPageLayout = (event) => {
            const {width, height} = event.nativeEvent.layout;
            this.setState({
                width,
                height
            })
        };

          render(){
            return (
              <View style={styles.container}>
                <View style={[
styles.banner,
{
height:this.state.width*9/16
}
]}>

                </View>
                <View style={styles.items}>

                </View>
              </View>
            );
          }
        });
于 2018-07-06T02:34:57.060 回答