菜鸟教程小白 发表于 2022-12-11 22:06:14

ios - React Native 响应式 View Iphone x


                                            <p><p>我在为 Iphone X 设计样式时遇到了一个小问题,我使用了 react-native-responsive-screen 中的 widthPercentageToDP 和 heightPercentageToDP 来使 View 相似,在除 Iphone X 之外的大多数设备中它都可以正常工作,差别有点有点,但我想让 View 尽可能准确。</p>

<pre><code>&lt;View style={styles.container}&gt;
                &lt;ImageBackground source={require(&#39;../../assets/acc.png&#39;)} style={styles.bgImg}&gt;
                  &lt;View style={styles.headerView}&gt;
                  &lt;FontAwesome style={styles.setting}name=&#34;cog&#34; size={hp(&#39;4%&#39;)} color=&#34;#6B6466&#34; /&gt;
                  &lt;Text style={styles.headerText}&gt;My Account&lt;/Text&gt;
                  &lt;/View &gt;

                  &lt;View style={styles.imgView} &gt;
                        &lt;Image source={require(&#39;../../assets/user.png&#39;)} style={styles.accImg}/&gt;
                        &lt;Text style={styles.name}&gt; John doe&lt;/Text&gt;
                        &lt;Text style={styles.number}&gt; 123456789&lt;/Text&gt;
                  &lt;/View&gt;
                  &lt;View style={styles.bottomView}&gt;
                  &lt;View style={styles.bottomView2}&gt;

                        &lt;TouchableOpacity style={styles.inboxView}&gt;
                            &lt;Text style={styles.inboxNumber}&gt;12&lt;/Text&gt;
                            &lt;Text style={styles.inboxText}&gt;Inbox&lt;/Text&gt;
                        &lt;/TouchableOpacity&gt;
                        &lt;TouchableOpacity style={styles.inboxView}&gt;
                            &lt;Text style={styles.inboxNumber}&gt;17&lt;/Text&gt;
                            &lt;Text style={styles.inboxText}&gt;Sent&lt;/Text&gt;
                        &lt;/TouchableOpacity&gt;
                  &lt;/View&gt;
                  &lt;/View&gt;
                &lt;/ImageBackground&gt;
            &lt;/View&gt;
</code></pre>

<p>风格</p>

<pre><code>const styles = StyleSheet.create({
    container: {
      flex: 1, backgroundColor: &#39;#fff&#39;
    },
    bgImg:{
      height: hp(&#39;40%&#39;), width: &#39;100%&#39;
    },
    setting:{
      color:&#39;white&#39;
    },
    headerText:{
      flex:1,textAlign:&#39;center&#39;,fontSize:wp(&#39;5%&#39;),fontFamily:Fonts.Cairo,marginRight:10,color:&#39;white&#39;
    },
    headerView:{
      flexDirection:&#39;row&#39;,justifyContent:&#39;space-between&#39;,marginHorizontal:10,marginTop:hp(&#39;5%&#39;)
    },
    imgView:{
      flex:1,
      alignItems:&#39;center&#39;
    },
    accImg:{
      height:wp(&#39;30%&#39;),width:wp(&#39;30%&#39;),borderRadius:wp(&#39;15%&#39;),marginTop:wp(&#39;3%&#39;)
    },
    name:{
      color:&#39;white&#39;,fontFamily:Fonts.Cairo,fontSize:wp(&#39;4%&#39;),textAlign:&#39;center&#39;
    },
    number:{
      color:&#39;white&#39;,fontSize:wp(&#39;4%&#39;),textAlign:&#39;center&#39;
    },
    bottomView:{
      flex:1,
      justifyContent:&#39;flex-end&#39;
    },
    inboxNumber:{
      color:&#39;white&#39;,
      textAlign:&#39;center&#39;,
      fontSize:wp(&#39;4%&#39;)
    },
    inboxText:{
      color:&#39;white&#39;,
      textAlign:&#39;center&#39;,
      fontSize:wp(&#39;4%&#39;),
      fontFamily:Fonts.Cairo
    },
    bottomView2:{
      flexDirection:&#39;row&#39;,
      justifyContent:&#39;space-between&#39;,
      marginHorizontal:10,
      marginBottom:wp(&#39;3%&#39;)
    },

});
</code></pre>

<p>输出
<a href="/image/vmGxg.png" rel="noreferrer noopener nofollow"><img src="/image/vmGxg.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Iphone X 正在渲染真实的结果,这就是您的布局的样子,其他设备没有足够的空间进行渲染,并且它们“重叠”了 View ......看看你的 View ,你有一个图像背景它占据了 40% 的屏幕,里面有一个可以正常渲染的标题,因为它在渲染功能中更高。</p>

<p>下面你有一个带有 flex :1 的 View 和一个基于屏幕值的图像,下面有 2 行文本,如果图像大于 View ,它将把文本推到 View 之外,侵入下面的邻居 View </p>

<p>在下面,您可以看到 2 touchableopacitys 的 View 。它们不应该重叠。其实试试在imgView样式中加入<code>overflow:'hidden'</code></p>

<p>------标题------</p>

<p>///////图片////////</p>

<p>///////图片////////</p>

<p>///////约翰多///////</p>

<p>///////12345////////</p>

<p>按钮//////按钮</p>

<p>就像 iphone 显示的一样</p>

<pre><code>&lt;ImageBackground source={require(&#39;../../assets/acc.png&#39;)} style={styles.bgImg}&gt;
                  &lt;View style={styles.headerView}&gt;
                  //here is the header
                  &lt;/View &gt;

                  &lt;View style={styles.imgView} &gt;
                     // below is the image , the name , and the number
                  &lt;/View&gt;
                  &lt;View style={styles.bottomView}&gt;
                  //below it should be the 2 buttons , not overlapping, BELOW
                  &lt;/View&gt;
                &lt;/ImageBackground&gt;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - React Native 响应式 ViewIphone x,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54389044/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54389044/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - React Native 响应式 View Iphone x