OStack程序员社区-中国程序员成长平台

标题: ios - 如何将 View 样式设置为 16 :9 in React Native? 之类的比率 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:29
标题: ios - 如何将 View 样式设置为 16 :9 in 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"
]"



Best Answer-推荐答案


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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4