繁体   English   中英

反应本机显示图像

[英]react native display image

大家好,我想显示一个图像,但我不知道为什么,我无法显示它。 我认为这是可能的,因为我使用了错误的高度和宽度,但我正在搜索,但没有找到任何解决方案。

有我的代码:

import React, {Component, useState, useEffect, useRef} from "react";
import {
  View,
  Text,
  Image,
  StyleSheet,
  useWindowDimensions,
  ScrollView,
  TouchableOpacity,
  Modal,
  Pressable,
  Alert,
  TextInput,
  Button,
  Linking,
  Animated,
  LayoutAnimation,
} from "react-native";
import {Formik} from "formik";
import {useDispatch} from "react-redux";
import * as yup from "yup";
import RenderHtml from "react-native-render-html";
import AsyncStorage from "@react-native-async-storage/async-storage";
<ScrollView>
      <View style={styles.descriptionPage}>
        <View
          style={{
            alignItems: "center",
            backgroundColor: "#fff",
            height: "25%",
          }}
        >
          <Pressable onPress={() => {}}>
            <Image
              style={{
                width: "20%",
                height: "20%",
              }}
              source={require("../../assets/images/cloche.png")}
            />
          </Pressable>
          <Image
            style={styles.imageProduct}
            source={{uri: data["image_url"]}}
          />
          <TouchableOpacity
            onPress={() => {
              navigation.goBack();
            }}
          >
            <Text style={styles.category}>{category}</Text>
          </TouchableOpacity>
          <Text style={styles.title}>{data["titre"]}</Text>
          <View style={{display: "flex", flexDirection: "row", marginTop: 7}}>
            <Text style={{color: "grey", top: 38}}>à partir de </Text>
            <Text style={styles.price}>{data["prix"]} €</Text>
          </View>
          <TouchableOpacity style={{top: "10%"}} onPress={toggleText}>
            <Text>Description Produit</Text>
          </TouchableOpacity>
        </View>
        {showText && (
          <ScrollView>
            <Animated.View
              style={{
                transform: [{scaleY: scaleYInterpolation}],
                height: "auto",
                width: "85%",
                left: "5%",
              }}
            >
              <RenderHtml contentWidth={width} source={short_description} />
            </Animated.View>
          </ScrollView>
        )}
        <View
          style={{
            backgroundColor: "#EBEBEB",
            right: "2%",
            width: "110%",
            bottom: "0.15%",
          }}
        >
          <Text
            style={{fontWeight: "bold", fontSize: 22, top: "2%", left: "5%"}}
          >
            {offerCpt} offres
          </Text>

          {offers.map((element, index) => {
            const date = new Date(element["updated_at"]);
            const dateOnly = date.toISOString().split("T")[0];
            return (
              <View style={styles.allOffer}>
                <Text
                  style={{
                    color: "#ff7c00",
                    fontSize: 25,
                    paddingLeft: 10,
                    fontWeight: "bold",
                    top: 7,
                  }}
                >
                  {element["prix"]} €
                </Text>
                <Text
                  style={{
                    paddingLeft: 10,
                    color: "grey",
                    fontSize: 11,
                    top: 5,
                  }}
                >
                  TVA incl.
                </Text>
                <Text
                  style={{
                    color: "#8F8A84",
                    paddingLeft: 10,
                    fontSize: 10,
                    top: "15%",
                    width: "45%",
                  }}
                >
                  Annonce valable aujourd'hui, mise à jour le : {dateOnly}
                </Text>
                <TouchableOpacity
                  onPress={() => Linking.openURL(element["url"])}
                >
                  <Text
                    style={{
                      left: "70%",
                      bottom: "55%",
                      color: "#ff7c00",
                      borderRadius: 20,
                      fontSize: 15,
                      borderColor: "#ff7c00",
                      borderWidth: 1,
                      width: "25%",
                      textAlign: "center",
                    }}
                  >
                    Voir l'offre
                  </Text>
                </TouchableOpacity>
              </View>
            );
          })}
        </View>
        <View style={styles.centeredView}>
          <Modal
            animationType="slide"
            transparent={true}
            visible={modalVisible}
            onRequestClose={() => {
              Alert.alert("Modal has been closed.");
              setModalVisible(!modalVisible);
            }}
          >
            <View style={styles.centeredView}>
              <View style={styles.modalView}>
                <Pressable style={{left: "50%"}}>
                  <Button
                    style={{left: "50%", color: "black"}}
                    onPress={() => setModalVisible(!modalVisible)}
                    title="X"
                    color={"#ff7b0d"}
                  />
                </Pressable>
                <Text style={{fontSize: 22, right: "25%", bottom: "5%"}}>
                  Prix trop élevé ?
                </Text>
                <Text style={styles.modalText}>Saisir le montant souhaité</Text>
                <Formik
                  initialValues={{
                    targetPrice: "",
                  }}
                  validationSchema={formSchema}
                  onSubmit={handleSubmit}
                >
                  {(props) => (
                    <View>
                      <TextInput
                        style={styles.input}
                        placeholderTextColor="#fff"
                        onChangeText={props.handleChange("targetPrice")}
                        value={props.values.targetPrice}
                      />
                      <Text
                        style={{
                          fontSize: 25,
                          color: "#ff7b0d",
                          left: "90%",
                        }}
                      >
                        €
                      </Text>
                      <Button
                        color="#ff7b0d"
                        title="Valider"
                        onPress={props.handleSubmit}
                      />
                    </View>
                  )}
                </Formik>
              </View>
            </View>
          </Modal>
        </View>
      </View>
    </ScrollView>

风格:

imageProduct: {
    alignSelf: "center",
    width: "50%",
    height: "50%",
    top: "5%",
  },
  category: {
    fontWeight: "bold",
    fontSize: 15,
    color: "#324160",
    marginTop: 10,
  },
  title: {
    fontWeight: "bold",
    fontSize: 26,
    marginTop: 10,
  },
  price: {
    color: "#ff7b0d",
    fontSize: 22,
    fontWeight: "bold",
    paddingLeft: 5,
  },

所以如果有人能解释我这个“问题”在哪里。 提前谢谢你我继续在我身边。

在您的代码中,您需要提供特定的数字。 或者使用带有特定百分比的 Pressable for ex。 100% 并将您的图像包裹在其中并为您的图像 img {width: 100%;} 样式。

所以答案是你需要给组件一个宽度

<Pressable style = {{width: "100%"}}> 
   <Image style={{ width: "20%", height: "20%"}} source={require('../../../picture.png')} /> 
</Pressable>

暂无
暂无

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

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