菜鸟教程小白 发表于 2022-12-13 10:29:01

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


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

<p>这是我的 Javascript:</p>

<pre><code>&#39;use strict&#39;;

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

var AwesomeProject = React.createClass({
render: function() {
    return (
      &lt;View style={styles.container}&gt;
      &lt;View style={styles.banner}&gt;

      &lt;/View&gt;
      &lt;View style={styles.items}&gt;

      &lt;/View&gt;
      &lt;/View&gt;
    );
}
});

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

AppRegistry.registerComponent(&#39;AwesomeProject&#39;, () =&gt; AwesomeProject);
</code></pre>

<p>这是关于 React Native 中的 Flexbox 的文档:</p>

<p> <a href="http://facebook.github.io/react-native/docs/flexbox.html#content" rel="noreferrer noopener nofollow">http://facebook.github.io/react-native/docs/flexbox.html#content</a> </p>

<p>这是有效的样式 Prop :</p>

<pre><code>Valid style props: [
&#34;width&#34;,
&#34;height&#34;,
&#34;top&#34;,
&#34;left&#34;,
&#34;right&#34;,
&#34;bottom&#34;,
&#34;margin&#34;,
&#34;marginVertical&#34;,
&#34;marginHorizontal&#34;,
&#34;marginTop&#34;,
&#34;marginBottom&#34;,
&#34;marginLeft&#34;,
&#34;marginRight&#34;,
&#34;borderWidth&#34;,
&#34;borderTopWidth&#34;,
&#34;borderRightWidth&#34;,
&#34;borderBottomWidth&#34;,
&#34;borderLeftWidth&#34;,
&#34;position&#34;,
&#34;flexDirection&#34;,
&#34;flexWrap&#34;,
&#34;justifyContent&#34;,
&#34;alignItems&#34;,
&#34;alignSelf&#34;,
&#34;flex&#34;,
&#34;resizeMode&#34;,
&#34;backgroundColor&#34;,
&#34;borderColor&#34;,
&#34;borderRadius&#34;,
&#34;tintColor&#34;,
&#34;opacity&#34;,
&#34;fontFamily&#34;,
&#34;fontSize&#34;,
&#34;fontWeight&#34;,
&#34;fontStyle&#34;,
&#34;lineHeight&#34;,
&#34;color&#34;,
&#34;containerBackgroundColor&#34;,
&#34;textAlign&#34;,
&#34;writingDirection&#34;,
&#34;padding&#34;,
&#34;paddingVertical&#34;,
&#34;paddingHorizontal&#34;,
&#34;paddingTop&#34;,
&#34;paddingBottom&#34;,
&#34;paddingLeft&#34;,
&#34;paddingRight&#34;,
&#34;borderTopColor&#34;,
&#34;borderRightColor&#34;,
&#34;borderBottomColor&#34;,
&#34;borderLeftColor&#34;,
&#34;overflow&#34;,
&#34;shadowColor&#34;,
&#34;shadowOffset&#34;,
&#34;shadowOpacity&#34;,
&#34;shadowRadius&#34;,
&#34;transformMatrix&#34;,
&#34;rotation&#34;,
&#34;scaleX&#34;,
&#34;scaleY&#34;,
&#34;translateX&#34;,
&#34;translateY&#34;
]&#34;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>React Native(从 0.40 开始)支持 <a href="https://facebook.github.io/react-native/docs/layout-props#aspectratio" rel="noreferrer noopener nofollow"><code>aspectRatio</code></a> Prop 。</p>

<p>你可以这样做:</p>

<p><code>style={{ aspectRatio: 16/9 }}</code></p>

<p>见 <a href="https://stackoverflow.com/questions/29642685/maintain-aspect-ratio-of-image-with-full-width-in-react-native" rel="noreferrer noopener nofollow">Maintain aspect ratio of image with full width in React Native</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何将 View 样式设置为 16 :9 in React Native? 之类的比率,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/29447207/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/29447207/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何将 View 样式设置为 16 :9 in React Native? 之类的比率