【发布时间】:2021-07-16 08:13:44
【问题描述】:
我正在开发一个简单的 react native 应用程序。我刚开始使用本机反应。我正在使用最新版本 0.64。问题是 ImageBackground 的 resizeMode 在样式表中使用时不起作用。但是,当我将它用作 ImageBackground 组件的直接道具时,它开始工作。
以下是我的代码:
/*global require */
...
import {
StyleSheet,
ImageBackground,
View,
TouchableOpacity,
Text,
useWindowDimensions,
} from "react-native";
...
export default function LaunchScreen({ navigation }) {
...
const image = require("../../assets/test.png");
...
return (
<View onLayout={handleLayout} style={styles.container}>
<ImageBackground source={image} style={styles.image}>
...
</ImageBackground>
</View>
);
}
const styles = StyleSheet.create({
...
image: {
flex: 1,
resizeMode: "contain",//--> This is not working.
justifyContent: "center",
},
...
});
LaunchScreen.propTypes = {
navigation: PropTypes.object.isRequired,
};
如果我将这个 resizeMode 属性添加到 ImageBackground,它就会开始工作。根据文档 https://reactnative.dev/docs/imagebackground,它也应该在样式表中工作。知道为什么会这样吗?以及解决方案是什么
【问题讨论】:
-
它是否与其他 resizeMode 值一起使用?
-
不,不是。不在样式表中。
-
尝试为图像指定高度和宽度,而不是
flex:1...它应该可以使用。 -
试过了,没用。可能是它的某种错误。
-
试试这个stackoverflow.com/a/49309853/3237884@BasitMinhas