【发布时间】:2018-08-09 15:21:54
【问题描述】:
每当我将此视图的位置样式设置为“绝对”时,它就会消失。我不知道为什么会这样。
import React, { Component } from 'react';
import { Text, View, Image, TouchableOpacity, StyleSheet } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.mainView}>
<View
style={styles.parentView}>
<TouchableOpacity
activeOpacity={0.9}
onPress={() => {}}>
<View
style={ [{position: 'absolute'}, styles.textView] }>
<Text style={styles.textViewText}>
Just Another Cat
</Text>
</View>
<Image
style={[{position: 'absolute'}, styles.imageView]}
source={{ uri: 'https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg' }}
/>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
mainView: { flexDirection: 'row', flex: 1 },
parentView: { alignSelf: 'center', flex: 0.5, left: 10, elevation: 3, alignItems: 'flex-start' },
textView: { width: 250, height: 250, opacity: 1, borderRadius: 125, backgroundColor: '#aaa034' },
textViewText: { textAlign: 'center', padding: 10, color: '#ffffff', fontSize: 18, top:0, right:0, bottom:0, left:0, textAlignVertical: 'center', position: 'absolute' },
imageView: { width: 250, height: 250, borderRadius: 125, borderColor: '#000000', borderWidth: 0.2 }
});
当我删除与styles.textView 和styles.imageView 一起添加的inline-style: (position: 'absolute') 时,它们都将再次可见。但出于某种原因,我想使该位置成为绝对位置,但随后它们就消失了。我不知道为什么。谁能解释一下?
编辑:它适用于 iOS。它在android上给出了这个错误。小吃网址:https://snack.expo.io/Hy_oRrvOM
【问题讨论】:
-
您必须添加一些
left, right, top, bottom属性,不是吗?最后一个。见:stackoverflow.com/questions/29541971/… -
试过了,还是不行。
-
我已将您的代码放入snack here。这是预期的行为吗?我不确定我看到的是否和你一样
-
它是空白的。它应该显示图像/选项文本,就像它们不是绝对的一样。你看到图像了吗?编辑:刚刚检查过,它适用于iOS,但不适用于android。在 iOS 上运行代码,您会看到“猫”图像,但在 android 上运行时,它不会显示任何内容。
标签: react-native