css React Native -调整图像大小组件

jk9hmnmh  于 2023-10-21  发布在  React
关注(0)|答案(2)|浏览(132)

我在React Native中有一个应用程序,我试图在我的应用程序中引入一个图像,但是图像太大了,图像占据了整个屏幕。为了弥补这一点,

imagem:{
    flex: 1,
    width: null,
    height: null,
    resizeMode: 'contain'
  }

图像本身的大小很好,但是组件太大了,所以很难看到其他组件。
完整的脚本在这里:

import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { SafeAreaView, StyleSheet, Text, View, Button, Image } from 'react-native';

export default function App() {  
const moedas = [
    require('./imagens/m1.png'),
    require('./imagens/m5.png'),
    require('./imagens/m1.png'),
    require('./imagens/m2.png'),
    require('./imagens/m3.png'),
    require('./imagens/m4.png'),
    require('./imagens/m5.png'),
    require('./imagens/m6.png'),
    require('./imagens/m7.png'),
    require('./imagens/m8.png'),
  ]

  let iMoeda = 0
  const maxGiros = 20

  const [moedaAtual,setMoedaAtual] = useState(moedas[iMoeda])

  async function espera(tmp) {
    function tempo(ms) {
      return new Promise(resolve => setTimeout(resolve,ms))
    }
    await tempo(tmp)
  }

  async function girarMoeda() {
    
  }  
return (
    <SafeAreaView style={estilos.container}>
      <View>
        <Text>Cara ou Coroa</Text>
        <Image style={estilos.imagem} source={moedaAtual}/>
        <Button
          title='Girar'
          onPress={()=>{}}
        />
      </View>
    </SafeAreaView>
  );
}

const estilos = StyleSheet.create({
  container:{
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  imagem:{
    flex: 1,
    width: null,
    height: null,
    resizeMode: 'contain'
  }
});
wkyowqbh

wkyowqbh1#

您可以删除Flex:1属性从estilos.imagem

const estilos = StyleSheet.create({
  container:{
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  imagem:{
    width: null,
    height: null,
    resizeMode: 'contain'
  }
});
ma8fv8wu

ma8fv8wu2#

width设置为100%,将height设置为固定大小(为其他组件留出空间),然后包含或覆盖resizeMode以获得所需的结果。

<Image
  style={{ width: '100%', height: 400, resizeMode: 'cover' }}
  source={{
    uri: 'https://picsum.photos/1920/1080',
  }}
/>

相关问题