【问题标题】:React Native FlexLayout: Alignment item rightReact Native FlexLayout:向右对齐项目
【发布时间】:2018-09-15 11:38:18
【问题描述】:

我有一个产品列表,想在我的信息框右端显示文本“ITEM UNIT”。然而,它会受到其上方文本位置的影响。

如何解决这个问题并将“ITEM UNIT”放在显示屏的右端?

<TouchableOpacity
onPress={() => this.props.onItemClicked(this.props.item)}
style={{ marginRight: 130 }}
>
<View style={styles.itemContent}>
    <View>
        <FadeIn>
            <Image
                resizeMode="contain"
                style={{
                    height: 100,
                    width: 100,
                    marginTop: 10,
                    marginLeft: 10
                }}
                source={{ uri: url }}
            />
        </FadeIn>
    </View>
    <View style={{ justifyContent: "space-around" }}>
        <Text style={{ fontSize: 16, fontFamily: "System" }}>
            {articleName}
        </Text>
        <View
            style={{
                flexDirection: "row",
                justifyContent: "space-between"
            }}
        >
            <View style={{ flexDirection: "row" }}>
                <Text style={styles.itemPrice}>{originalprice} </Text>
                <Text style={styles.itemPriceReduced}>{specialprice}€</Text>
            </View>
            <View>
                <Text>ITEMUNIT</Text>
            </View>
        </View>
    </View>
</View>
</TouchableOpacity>;

【问题讨论】:

    标签: reactjs react-native flexbox react-native-flexbox


    【解决方案1】:

    我为您制作了一个小演示。请在此处查看: https://snack.expo.io/@tarikfojnica/layout(点击右侧点击播放)

    以下是您应该检查的代码部分:

    <TouchableOpacity style={styles.container} onPress={() => this.props.onItemClicked(this.props.item)}>
      <View style={styles.itemContent}>
        <View style={styles.iconContainer}>
          <Image source={Icon} style={styles.icon} />
        </View>
        <View style={styles.textContainer}>
          <Text>This is the title </Text>
    
          <View style={styles.downText}>
            <View style={styles.priceText}>
              <Text style={{marginRight: 10}}>$99.9</Text>
              <Text>$99.9</Text>
            </View>
    
            <View style={styles.label}>
              <Text>Label</Text>
            </View>
          </View>
        </View>
      </View>
    </TouchableOpacity>
    
    const styles = StyleSheet.create({
      container: {
        marginTop: 24,
      },
    
      itemContent:  {
         flexDirection: 'row',
         borderBottomWidth: 1,
         borderColor: 'e5e5e5'
      },
    
      iconContainer: {
        padding: 10,
        flex: 1,
      },
    
      icon: {
        width: 40,
        heigth: 40
      },
    
      textContainer: {
        backgroundColor: 'whitesmoke',
        flex: 7,
        padding: 5
      },
    
      downText: {
        marginTop: 10,
        flexDirection: 'row',
        justifyContent: 'space-between'
      },
    
      priceText: {
        flexDirection: 'row',
      },
    
      label: {
        textAlign: 'right',
        backgroundColor: 'yellow',
        padding: 3
      }
    });
    

    作为参考,如下所示:

    PS:我会避免编写内联样式。

    【讨论】:

    • 嘿塔里克。非常感谢您的回答。您的代码实际上优于我的代码。只有一件事。如果图标比您的示例高得多,则价格文本未与底部对齐。有什么办法可以存档吗?
    • 请再次检查。我做了一些更改以满足您的新要求:代码:snack.expo.io/@tarikfojnica/layout 截图:i.imgur.com/TODSqDy.png
    • 太棒了我的英雄!你拯救了我的一天。
    猜你喜欢
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 2016-09-04
    • 2022-12-18
    • 2018-07-06
    相关资源
    最近更新 更多