React Native 为什么我的图像从网址没有出现在TouchableOpacity

amrnrhlw  于 2022-12-14  发布在  React
关注(0)|答案(2)|浏览(180)

在react-native中,我尝试创建一个滚动视图,其中每个元素都有一个标题和一个图片。因为我想从url加载图片,所以我写了下面的代码:

import {Text, TouchableOpacity,Image } from 'react-native'
import React from 'react'

const CatagoryCard = ({imgUrl,title}) => {
  return (
    <TouchableOpacity>
        <Image
            source = {{uri:imgUrl}}
            resizeMode = 'contain'
            className = "h-20 w-20 rounded flex-2"
        />
      <Text>{title}</Text>
    </TouchableOpacity>
  );
};

export default CatagoryCard;

并从另一个父组件类调用它们。

import { View, Text, ScrollView } from 'react-native'
import React from 'react'
import CatagoryCard from './CatagoryCard'

const Catagories = () => {
  return (
    <ScrollView horizontal
    showsVerticalScrollIndicator={false}
    contentContainerStyle={{
      paddingHorizontal:15,
      paddingTop:10
    }}>
      <CatagoryCard imgUrl = "https://i.ibb.co/ZYvGfFY/Untitled-design-7.png" title = " TEST 1"/>
      <CatagoryCard imgUrl = "https://i.ibb.co/ZYvGfFY/Untitled-design-7.png" title = " TEST 2"/>
      <CatagoryCard imgUrl = "https://i.ibb.co/ZYvGfFY/Untitled-design-7.png" title = " TEST 3"/>

    </ScrollView>
  )
}

export default Catagories

问题是标题在视图中的各个元素中显示得很好,但由于一些未知的原因,图像没有加载。

jk9hmnmh

jk9hmnmh1#

设定图像大小:

<TouchableOpacity>
  <Image
    style={{height: 50, width: 50}}
    source={{
      uri: 'https://reactnative.dev/img/tiny_logo.png',
    }}
  />
  <Text>Text</Text>
</TouchableOpacity>
zbwhf8kr

zbwhf8kr2#

用这个它对我很有效,

import { View, Text, TouchableOpacity, Image } from 'react-native'
import React from 'react'

const CategoryCard = ({imgUrl, title}) => {
  return (
    <TouchableOpacity className="mr-2 relative">
        <Image style={{height: 50, width: 50}} source= {{ uri: imgUrl }} />
      <Text className="bottom-1 left-1 absolute">{title}</Text>
    </TouchableOpacity>
  )
}

export default CategoryCard

我觉得顺风css有问题

相关问题