繁体   English   中英

如何使视图高度适合 React Native 中的内容?

[英]How to make a view height fit the content inside in react native?

我正在构建一个移动聊天应用程序,我遇到了这个问题,我将消息显示为视图内的文本,但显然我无法使消息显示在适合视图内的特定高度。 要么是消息太长,无法完整显示(视图中没有足够的空间),要么是消息出现了,但文本后面有很多空格。

请查看准确描述问题的图像:

在此处输入图像描述

这是我的代码:

1- 意见:

if (chat.from !== CurrentUser) {
    temp_chat.push(
      <View key={i} style={styles.messageContainer1}>
        <View style={styles.thisUserText}>
          <Text style={styles.message}>{chat.msg}</Text>
        </View>
        <View style={styles.empty}></View>
      </View>
    );
  } else {
    temp_chat.push(
      <View key={i} style={styles.messageContainer2}>
        <View style={styles.empty}></View>
        <View style={styles.otherUserText}>
          <Text style={styles.message}>{chat.msg}</Text>
        </View>
      </View>
    );
  }

2-风格:

messageContainer1: {
flexDirection: "row",
marginBottom: 10,
flex: 1,
},
messageContainer2: {
flexDirection: "row",
justifyContent: "flex-end",
marginBottom: 10,
flex: 1,
},
message: {
fontSize: 16,
color:"#fff",
padding:10
},
empty: {
flex: 1,
},
thisUserText: {
backgroundColor: "#bf0010", // red 
flex: 1,
height: 100,
borderRadius: 5,
color:"#fff",

},
otherUserText: {
backgroundColor: "#13283E", // dark blue
flex: 2,
height: 100,
borderRadius: 5,
},

我需要的是红色和深蓝色视图的动态高度,使里面的文本无论是长文本还是短文本都能完美匹配。 我怎样才能做到这一点?

提前致谢

您可以使用flexBasis: auto

阅读此处了解更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM