【问题标题】:How set auto height for view in react native如何在本机反应中设置视图的自动高度
【发布时间】:2018-12-04 19:50:16
【问题描述】:

我有一个从 api 获取数据的 react 本机应用程序 在这个api中有新闻,每个都包含主题、图片和细节 我在将每个项目的高度设置为自动高度时遇到问题 我尝试将容器的高度设置为 100,然后主题和细节只是相互重叠,所以我将其设置为 300,对于具有长文本的项目没关系,但对于具有短文本的项目的问题,因此空间这些项目之间变得太大 那么如何设置自动高度,以便每个项目都具有与其内容相关的高度

所以这是每个项目的类:

import React, { Component } from "react"
import { View, Image, Text, StyleSheet } from "react-native"

export default class Item extends Component {
    render() {
        let { item } = this.props;
        const { subject, picture, details } = item;
        return (
            <View style={styles.container}>
                <Image style={styles.picture} resizeMode="cover" source={{ uri: picture }} />
                {console.log(image)}

                <View style={styles.content}>
                    <Text style={styles.subject}>{subject}</Text>
                    <Text style={styles.details}>{details}</Text>
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        height: 300,
        flexDirection: "row",
        flex: 1,
        padding: 10
    },
    content: {
        flexDirection: "column",
        flex: 1
    },
    picture: {
        height: 70,
        width: 100
    },
    subject: {
        marginLeft: 20,
        fontSize: 16,
        fontWeight: 'bold'
    },
    details: {
        marginLeft: 20,
        fontSize: 16,
    }
})

【问题讨论】:

  • 尝试在你的主容器视图中使用 flex 来探索你的组件

标签: react-native


【解决方案1】:

跳过为容器提供高度,您将获得动态所需的高度,并从图像中删除高度,以便它根据正在呈现的文本占据最终容器高度的高度。

您的样式表应如下所示:

const styles = StyleSheet.create({
    container: {
        flexDirection: "row",
        //flex: 1, - remove this as this doesn't make any sense here.
        padding: 10
    },
    content: {
        flexDirection: "column",
        flex: 1,
        paddingLeft: 10, //if you need separation.
    },
    picture: {
        //height: 70, - commenting this will make it automatically ataining height as per your text grows.
        width: 100
    },
    subject: {
        marginLeft: 20,
        fontSize: 16,
        fontWeight: 'bold'
    },
    details: {
        marginLeft: 20,
        fontSize: 16,
    }
})

【讨论】:

  • 感谢它在我从容器中删除高度后现在可以工作,但我保留了图片中的高度,因为它们在评论高度时拉伸@Suraj Malviya
  • 除非需要,否则永远不要使用 cover 作为 resizeMode,对于简单的用例,contain 被证明是最佳选择。
  • 好的,但是当我将它设置为包含图片时,图片不在每个项目的顶部,而是图片现在位于行文本的中心,那么我怎样才能使其位于主题旁边的顶部? @Suraj Malviya
  • 使用与 Image 相同样式的 View 包装图像,并使用 flex:1resizeMode: contain 将 Image 作为该视图的子视图。它应该把图像放在左上角。还要使其顶部居中对齐,在我声明的新视图中插入alignItems: center
  • 我这样设置&lt;View style={styles.itemNewsContainer}&gt; &lt;View style={styles.image}&gt; &lt;Image style={{flex: 1}} resizeMode="contain" source={{ uri: urlToImage }} /&gt; &lt;/View&gt; &lt;View style={styles.content}&gt; &lt;Text style={styles.title}&gt;{title}&lt;/Text&gt; &lt;Text style={styles.description}&gt;{description}&lt;/Text&gt; &lt;Text style={styles.publishedAt}&gt;{publishedAt}&lt;/Text&gt; &lt;/View&gt; &lt;/View&gt;,但没有像预期的那样工作
猜你喜欢
  • 1970-01-01
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
相关资源
最近更新 更多