【问题标题】:React Native: Text wrapping inside View container with fixed-width buttonReact Native:带有固定宽度按钮的视图容器内的文本换行
【发布时间】:2016-11-22 00:39:32
【问题描述】:

我有一个 View 容器,它显示一条简单的警告消息。消息文本应该设置换行,按钮不应该。

它们不能很好地配合,我无法让文本在每一行的末尾换行。

RNPlay (Link)

<View style={{backgroundColor: 'red',flexDirection:'row',justifyContent:'space-between',alignItems:'center',flexWrap:'nowrap'}}>
  <View style={{justifyContent: 'flex-start', alignItems:'center', marginLeft:15, flexWrap: 'wrap'}}>
    <Text style={{color:'white', fontSize: 18, fontWeight:'normal', flexWrap: 'wrap'}}>You have deleted the item.</Text>
  </View>
  <TouchableHighlight style={{width:100,flexDirection:'column',marginVertical:15,flexWrap:'nowrap'}} >
    <View style={{right:0,width:100, flexDirection:'row', marginRight:15, paddingVertical:15, backgroundColor:'white',flexWrap:'nowrap'}}>
      <Text style={{fontSize: 18, fontWeight:'bold', color:'red',flexWrap:'nowrap'}}>BACK</Text>
    </View>
  </TouchableHighlight>
</View>

【问题讨论】:

    标签: reactjs flexbox word-wrap react-native


    【解决方案1】:

    主要思想是使用Dimensions 并设置主容器的宽度。 喜欢:

    import Dimensions from 'Dimensions';
    let {width} = Dimensions.get('window');
    

    然后在主组件的样式中:

    <View style={{backgroundColor: 'red', ... , width}}>
    

    在这里工作: https://rnplay.org/apps/SampHw

    【讨论】:

    • 但是这个文本换行也应该在与主窗口宽度不同的其他容器中工作,希望你能理解我。
    • @PeterGerhat 它应该可以工作,你可以尝试附上具体的例子吗?
    • 好的,看起来问题是我将&lt;View&gt; 创建为var msgBox = &lt;View&gt;...&lt;/View&gt;; 中的变量,但它没有正确解释宽度。现在它在将&lt;View&gt; 直接放入return (&lt;View&gt;...&lt;/View&gt;); 之后就可以工作了。
    猜你喜欢
    • 2021-12-02
    • 2017-11-15
    • 2010-10-26
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多