可以在this live example中找到以下代码

我有以下元素:

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var SampleApp = React.createClass({
  render: function() {
    return      (
  <View style={styles.container}>

        <View style={styles.descriptionContainerVer}>
          <View style={styles.descriptionContainerHor}>
            <Text style={styles.descriptionText} numberOfLines={5} >
              Here is a really long text that you can do nothing about, its gonna be long wether you like it or not, so be prepared for it to go off screen. Right? Right..!
            </Text>
          </View>
        </View>

        <View style={styles.descriptionContainerVer2}>
          <View style={styles.descriptionContainerHor}>
            <Text style={styles.descriptionText} numberOfLines={5} >Some other long text which you can still do nothing about.. Off the screen we go then.</Text>
          </View>
        </View>



  </View>);
  }
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);

具有以下样式:

var styles = StyleSheet.create({
  container:{
        flex:1,
    flexDirection:'column',
        justifyContent: 'flex-start',
        backgroundColor: 'grey'
    },
    descriptionContainerVer:{
    flex:0.5, //height (according to its parent)
    flexDirection: 'column', //its children will be in a row
    alignItems: 'center',
    backgroundColor: 'blue',
    // alignSelf: 'center',
  },
  descriptionContainerVer2:{
    flex:0.5, //height (according to its parent)
    flexDirection: 'column', //its children will be in a row
    alignItems: 'center',
    backgroundColor: 'orange',
    // alignSelf: 'center',
  },
  descriptionContainerHor:{
    //width: 200, //I DON\'T want this line here, because I need to support many screen sizes
    flex: 0.3,  //width (according to its parent)
    flexDirection: 'column',    //its children will be in a column
    alignItems: 'center', //align items according to this parent (like setting self align on each item)
    justifyContent: 'center',
    flexWrap: 'wrap'
  },
  descriptionText: {
    backgroundColor: 'green',//Colors.transparentColor,
    fontSize: 16,
    color: 'white',
    textAlign: 'center',
    flexWrap: 'wrap'
  }
});

这将导致出现以下屏幕:

Text off screen app

How can I stop the text from going off the screen and keep it confined in the middle of the screen with a width of i.e. 80% of the parent.

我不认为我应该使用width,因为我将在许多不同的移动屏幕上运行它,我希望它是动态的,所以我认为我应该完全依赖flexbox.

(这就是我在descriptionContainerHor分内得flex: 0.8分的最初原因.

我想要实现的是这样的:

What I want to achieve

非常感谢.

推荐答案

这个问题的解决方案是flexShrink: 1.

<View
    style={{ flexDirection: 'row' }}
> 
   <Text style={{ flexShrink: 1 }}>
       Really really long text...
   </Text>
</View>

根据您的设置,您可能还需要在<View>的父级上添加flexShrink: 1,才能使其正常工作,所以您可以try 一下,这样您就可以成功了.

this thread.年,Adam Pietrasiak人发现了这种解决方案

Css相关问答推荐

容器内的中心固定元件

Css-如何隐藏WP管理菜单上没有css类的第4<;li&>

更改MAT-FORM-FIELD的CSS,使其在有Angular 的HTML中显示为普通文本框

Astro网站在使用Astro Build构建后无法正常工作

MUI Textfield-悬停时输入标签 colored颜色

如何将React应用程序主页的内容居中?

如何为 MUI Slider 字段中 markLabel 的第一个和最后一个实例提供边距?

显示 P 元素中加载数据的空行

为什么删除 AnimatePresence framer.div 时会出现轻微卡顿?

同时实现渐变文本和闪光文本效果是否可行?

有没有办法在 CSS 字体速记中使用数字字体粗细?

Tailwind CSS 隐藏和可见

更改组件中 css 的值

如何从 表格单元格中创建链接

边框半径 + 背景 colored颜色 == 裁剪边框

使用 CSS3 生成重复的六边形图案

删除所有填充和边距表格 HTML 和 CSS

div内的CSS中心内容

在 CSS 中表示 100% 的 1/3 的最佳方式是什么?

更改 html 列表中项目符号的 colored颜色 ?