【问题标题】:I need an image to take up the maximum space it can regardless of its original size我需要一张图片来占用它可以占用的最大空间,无论其原始大小如何
【发布时间】:2019-07-13 05:02:13
【问题描述】:

我正在开发一个 react-native 应用程序,我有一个顶栏和一个底栏。在这两个条之间有一个图像,我需要它占用它可以占用的最大空间,而不管图像的原始大小如何,而不会妨碍两个条。我已经尝试了一段时间,但我无法弄清楚如何做到这一点。

我已经尝试了很多使用 flexbox 和设置图像大小的方法,但我无法让它工作。

import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
import Topbar from './topbar.js'

export default function App() {
  return (
    <View style = {{
      flex: 1,
      flexDirection: 'column',
      justifyContent: 'space-between'
    }}>
    <View>
      <Topbar />
    </View>
    <View>
      <Image source={require('./image2.jpg')} />
    </View>
    <View>
      <Topbar />
    </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

// here are my bars

import React, { Component } from "react";
import {
  Image, StyleSheet, View, Text,Dimensions
} from "react-native";
import Animated from "react-native-reanimated";


const {screenwidth, screenheight } = Dimensions.get("window");

export default class Topbar extends Component {
render() {
  return (
    <View style ={{
      width: screenwidth,
      height: 80,
      backgroundColor: "#C0C0C0",
      flexDirection: 'row',
      justifyContent: 'space-between',
    }}>
    </View>
  )
}
}

const styles = StyleSheet.create({
  topbarbackground: {
    width: screenwidth,
    height: 80,
    backgroundColor: "#C0C0C0",
  }

})

我需要查看条形图和图像,并且它必须占据整个手机屏幕。

【问题讨论】:

标签: css react-native flexbox


【解决方案1】:

我不太确定你想要得到什么。您可以尝试使用ImageBackground 组件并将resizeMode 设置为cover。示例:

覆盖整个屏幕

<ImageBackground 
    source={img}
    imageStyle={{ resizeMode: 'cover' }}
    style={{ width: '100%', height: '100%' }}>
  <Topbar/>
  <View style={{ flex: 1 }}/>
  <Bottombar/>
</ImageBackground>

覆盖可用空间

<View style={{ flex: 1 }}>
  <Topbar/>
  <ImageBackground 
    source={img}
    imageStyle={{ resizeMode: 'cover' }}
    style={{ width: '100%', flex: 1 }}/>
  <Bottombar/>
</View>

【讨论】:

  • 抱歉,世博会暂停了。谢谢!我现在就试试。
  • 好的,很乐意为您提供帮助!
猜你喜欢
  • 2021-11-08
  • 2016-11-05
  • 2021-04-02
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
相关资源
最近更新 更多