ios - 如何将 View 样式设置为 16 :9 in React Native? 之类的比例

标签 ios facebook reactjs flexbox react-native

我想让红色 View 保持 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

这是有效的样式 Prop :

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"
]"

最佳答案

React Native(自 0.40 起)支持 aspectRatio Prop 。

你可以这样做:

style={{ aspectRatio: 16/9 }}

参见 Maintain aspect ratio of image with full width in React Native

关于ios - 如何将 View 样式设置为 16 :9 in React Native? 之类的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29447207/

相关文章:

ios - TestFlight 无法更新分发配置文件

ios - Facebook "apprequest"对话框要求 facebook 登录,因为应用程序已经将其登录到 facebook

node.js - 从 fetchData 返回的 Promise 被忽略

ios - 在 iOS 设备上查找正确的时间

ios - 以编程方式向 View 添加事件指示器

javascript - 多用户 Facebook 共享器

php - 标签 friend 照片

reactjs - react typescript : Correct types for Route location and children properties

reactjs - 如何更改 URL 而无需实际路由到 React 中的不同页面

ios - 如何检测 ALAssetLibrary 中的最后一项是照片还是视频?