代码之家  ›  专栏  ›  技术社区  ›  louiedooey

React Native中的重叠和定位元素

  •  0
  • louiedooey  · 技术社区  · 6 年前

    我正在尝试重叠线框中显示的元素,并定位它们。

    我试过了 -位置:“相对”,元素消失 -位置:“absolute”但alignItems:“center”不执行任何操作

    有人能帮忙找出遗漏了什么吗?

    Wireframe of Wishlist (ignore the different header; it was from an old version)

    This is what I get instead, even after using flex

    我已经附加了所有单独的代码。js文件,并省略了所有导入和导出语句。

    非常感谢。

    ----------- in WishlistDetail.js------------
    
    // Each Item on The Wishlist
    const WishlistDetail = () => {
      return (
        <View>
          <WishlistCard>
            <WishlistThumbnail />
            <WishlistThumbnailFilter />
            <WishlistPrice />
            <WishlistItemDetail />
          </WishlistCard>
        </View>
      );
    };
    
    
    ---------- in WishlistCard.js------------------
    
    
    // Creating WishlistCard
    const WishlistCard = (props) => {
      return (
          <View style={styles.containerStyle}>
            {props.children}
          </View>
      );
    };
    
    // WishlistCard Style
    const styles = StyleSheet.create({
      containerStyle: {
        borderWidth: 0.75,
        backgroundColor: 'white',
        borderColor: 'rgb(217, 217, 217)',
        height: 125                                         // ******* not too sure
      }
    });
    
    ---------- in WishlistThumbnail.js------------------
    
    const WishlistThumbnail = () => {
        const { wishlistThumbnailStyle } = styles;
        return (
          <View>
            <Image
              style={wishlistThumbnailStyle}
              source={{ uri: 'http://www.startwire.com/job-applications/logos/amazon.png' }}
            />
          </View>
        );
    };
    
    // All Styling
    const styles = StyleSheet.create({
        wishlistThumbnailStyle: {
          height: 95,
          width: 95,
          padding: 20,
          position: 'absolute',
          justifyContent: 'center'
        }
    });
    
    
    ---------- in WishlistThumbnailFilter.js------------------
    
    // Creating Wishlist Thumbnail Filter
    const WishlistThumbnailFilter = () => {
      return (
        <View style={styles.wishlistThumbnailFilterStyle} />
      );
    };
    
    // Image Filter Style - 146 125 192.2
    const styles = StyleSheet.create({
      wishlistThumbnailFilterStyle: {
        width: 160,
        borderTopColor: 'rgba(13, 13, 13, 0.05)',
        borderLeftColor: 'transparent',
        borderRightColor: 'transparent',
        borderTopWidth: 250,
        borderLeftWidth: 0,
        borderRightWidth: 90
      }
    });
    
    ---------- in WishlistPrice.js------------------
    
    const WishlistPrice = () => {
      const { textStyle, viewStyle } = styles;
      return (
                                                                        //INSERT PRICE HERE
        <View style={viewStyle}>
          <Text style={textStyle}>30€</Text>
        </View>
      );
    };
    
    // Wishlist Price  Style
    const styles = StyleSheet.create({
      viewStyle: {
        backgroundColor: 'transparent',
        padding: '3',
        // alignItems: 'flex-end',
        justifyContent: 'flex-end',
        position: 'absolute'
        // position: 'relative'
      },
      textStyle: {
        fontSize: 11.5,
        fontFamily: 'Bariol_Regular',
        color: 'rgb(127, 127, 127)'
      }
    });
    
    
    ---------- in WishlistItemDetail.js------------------
    
    // This contains both Wishlist Title and Wishlist Text
    const WishlistItemDetail = () => {
      const { wishlistItemDetailStyle, wishlistItemTitleStyle, wishlistItemTextStyle } = styles;
      return (
        <View style={wishlistItemDetailStyle}>
          <Text style={wishlistItemTitleStyle}>Wishlist Item Title</Text>
          <Text style={wishlistItemTextStyle}>Wishlist Item Text</Text>
        </View>
      );
    };
    
    // Header Style
    const styles = StyleSheet.create({
      wishlistItemDetailStyle: {
        backgroundColor: 'transparent',
        position: 'absolute',
        padding: 5
      },
      wishlistItemTitleStyle: {
        fontSize: 15,
        fontFamily: 'Bariol_Regular',
        color: 'rgb(51, 51, 51)'
      },
      wishlistItemTextStyle: {
        fontSize: 12,
        fontFamily: 'Bariol_Regular',
        color: 'rgb(70, 70, 70)'
      }
    });
    1 回复  |  直到 6 年前
        1
  •  1
  •   flaky    6 年前

    虽然不想深入太多细节,但这似乎是一个 column vs公司 row 在你身上 flexDirection

    为了获得您提供的线框的样式,您需要将两者结合使用 一行 .查看此文档: https://facebook.github.io/react-native/docs/flexbox.html

    enter image description here

    因此,一行由两个在flexrow中设置样式的主视图组成,而我标记为蓝色的视图在列中设置样式(这是默认设置)。

    希望这有帮助