首页 > 解决方案 > React Native 样式问题

问题描述

我在嵌套视图中的 React Native 中的样式有问题。

期望的结果: 在此处输入图像描述

我的设计 : 在此处输入图像描述

如您所见,我在水平分配文本时遇到问题

代码:

let width = Dimensions.get('window').width - 42;

const styles = StyleSheet.create({
  cardBorder: {
    marginTop: 25,
    marginLeft: 20,
    marginRight: 20,
    borderColor: "grey",
    borderWidth: 1,
    borderRadius: 5,
    overflow: 'hidden',
  },
  titleText: {
    ...FONT_SEMI_BOLD,
    marginTop: 10,
    marginLeft: 10,
    fontSize: 17,
    flex: 1,
  },
  priceText: {
    ...FONT_SEMI_BOLD,
    marginTop: 10,
    marginLeft: 10,
    marginRight: 10,
    fontSize: 17,
    alignSelf: 'flex-end',
    flex: 1,
  },
});

export default class CardDetailScreen extends Component {
  render() {
    return (
      <SafeAreaView>
        <HeaderReturn />
        <View style={styles.cardBorder}>
          <Image
            style={{ height: width, width: width }}
            source={{ uri: testData[0].uri }}
          />
          <View
            style={{
              flexDirection: 'row',
              justifyContent: "space-between",
            }}
          >
            <Text style={styles.titleText}>{testData[0].name}</Text>
            <Text style={styles.priceText}>{testData[0].price}</Text>
          </View>
        </View>
      </SafeAreaView>
    );
  }
}

非常感谢任何帮助,它看起来超级简单,但我无法让它工作。谢谢!

标签: react-nativestylingreact-native-stylesheet

解决方案


似乎您想在视图的左侧显示名称并在右侧显示价格。所以你可以这样试试

priceText: {
    ...FONT_SEMI_BOLD,
    marginTop: 10,
    marginLeft: 'auto',
    marginRight: 10,
    fontSize: 17,
},

只需提及您的marginLeft: 'auto'. 还要justifyContent: "space-between",从父视图的样式中删除。


推荐阅读