【问题标题】:Cannot render View with position absolute on Android with react-native无法使用 react-native 在 Android 上以绝对位置渲染视图
【发布时间】:2019-07-29 16:04:18
【问题描述】:

我正在浪费时间试图弄清楚为什么在 Android 上我不能在父视图中绝对呈现带有图像的视图。

这是我喜欢渲染的卡片,它在 iOS 上正确显示。
我已经用position: 'absolute' 突出显示了我想渲染的两个元素,左上角的“礼品卡”横幅和父元素中心的一排点。

在 Android 上,这两个元素在白卡后面重新渲染,如您在此处看到的(我增加了 border-radius 以更好地显示它):

这是视图的代码:


import React from 'react';
import {
  TouchableWithoutFeedback,
  ImageBackground,
  View,
  Image,
  Text,
} from 'react-native';
import PropTypes from 'prop-types';
import { Colors, Metrics, Images, Fonts } from '../../Themes';

const VoucherCard = ({
  active,
  logo,
  onPress,
  value,
  credits,
  style,
  redeemed
}) => {
  let logoUrl = { uri: logo };

  return (
    <TouchableWithoutFeedback onPress={onPress}>
      <View style={[styles.container, style]}>
        <View style={styles.logoContainer}>
          <View
            style={{ flex: 1, borderRadius: 50, backgroundColor: Colors.transparent }}>
            <Image
              source={logoUrl}
              style={{ flex: 1, height: 80 }}
              imageStyle={{ borderRadius: 50 }}
              resizeMode={'contain'}
              defaultSource={Images.voucherPlaceholder}
            />
          </View>
        </View>
        <View style={{ ...styles.infoContainer, backgroundColor: Colors.white, }}>
          <View style={{ flex: 1, backgroundColor: redeemed ? Colors.backgroundSecondary : Colors.white, borderRadius: 5, justifyContent: 'center', }}>
            <Text style={{...Fonts.style.subTitle, textAlign: 'center', fontSize: 25, height: 30, color: redeemed ? Colors.grey : Colors.text}}>{`${credits}`}</Text>
            <Text style={{...Fonts.style.section, textAlign: 'center', color: redeemed ? Colors.grey : Colors.text}}>{`CREDITS`}</Text>
            <Text style={{...Fonts.style.miniText, textAlign: 'center', color: redeemed ? Colors.grey : Colors.text  }}>{`value ${value} €`}</Text>
          </View>
        </View>
        <View style={styles.dotsContainer}>
          <Image resizeMode='stretch' style={styles.dots} source={ Images.dots} />
        </View>
        <View style={{  position: 'absolute', top: 0, left: 0, zIndex: 100, }}>
          <Image
            source={Images.giftCardBanner}
            style={{ borderColor: 'blue', borderWidth: 2, }}
            />
        </View>
      </View>
    </TouchableWithoutFeedback>
  );
};

const styles = {
  container: {
    flexDirection: 'row',
    height: 90,
    flex: 1,
  },
  logoContainer: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: Colors.white,
    borderRadius: 50,
    elevation: 3,
    shadowColor: "grey",
    shadowOpacity: 0.2,
    shadowRadius: 2,
    shadowOffset: { height: 4, width: -2 },
    backgroundColor: Colors.white,
  },
  dotsContainer: {
    borderColor: 'red',
    borderWidth: 2,
    position: 'absolute',
    top: 0,
    right: 0,
    left: 0,
    bottom: 0,
    justifyContent: 'center',
    alignItems: 'center',
  },
  dots: {
    height: 90,
    width: 3,
  },
  infoContainer: {
    flex: 1,
    backgroundColor: Colors.white,
    borderRadius: 5,
    elevation: 3,
    shadowColor: "grey",
    shadowOpacity: 0.2,
    shadowRadius: 2,
    shadowOffset: { height: 4, width: 2 },
  },
}

export default VoucherCard;


【问题讨论】:

    标签: android css reactjs react-native


    【解决方案1】:

    zIndex只适用于IOS,安卓需要设置elevation

    <View style={{  position: 'absolute', top: 0, left: 0, zIndex: 100, elevation: 100 }}>
       <Image
          source={Images.giftCardBanner}
          style={{ borderColor: 'blue', borderWidth: 2, }}
        />
    </View>
    

    【讨论】:

    • 上帝保佑你这个互联网陌生人!
    • 谢谢你的互联网陌生人!!
    • 太棒了。这真的很棒。就我而言,只需 zIndex 适用于 iOS 和 Android
    猜你喜欢
    • 2019-02-26
    • 2018-08-09
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多