React Native - 当Android上的位置绝对时,视图会消失

Ket*_*tra 8 react-native

每当我为这个视图制作'绝对'的位置风格时,它就会消失.我不知道为什么会这样.

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 }
});
Run Code Online (Sandbox Code Playgroud)

当我删除与styles.textView和styles.imageView一起添加的内联样式:(位置:'绝对')时,它们都将再次可见.但是我想出于某种原因使这个位置绝对,但随后它们就消失了.我不知道为什么.有谁能解释一下?

编辑:它适用于iOS.它在android上给出了这个bug.小吃网址:https://snack.expo.io/Hy_oRrvOM

Jay*_*non 6

添加zIndex:100到您的属性。为我工作。不完全是,您只需为 zIndex 参数设置足够高的值,将其放在其他参数之上。


Ami*_*ndi 5

问题是的宽度和高度TouchableOpacity均为零!因为其中的内容是绝对的,所以您需要为其中一个子项设置(左,右,上,下),或者为其TouchableOpacity自身设置宽度和高度

为什么它可以在iOS中使用?
因为在iOS UIView中默认情况下不会裁剪其子级,所以实际上其TouchableOpacity宽度和高度仍为零,但子级溢出了,您可以看到它们,其中有一个标记(Docs)

但在android中ViewGroup总是剪辑其子级

如何解决这样的问题?
有一些工具可以在运行时调试UI,但是我认为react-native中最简单的方法是backgroundColor从根视图设置到目标视图!

因此,在这种情况下,例如:首先设置backgroundColorfor,mainView如果它确实显示,先设置backgroundColor为for parentView,然后再for ,因为的of 和of为零,TouchableOpacity您可以看到它没有显示,在每个步骤中,您都可以手动设置并确保问题backgroundColorTouchableOpacitywidthheightwidthheight