0

I have problems with styling in React Native inside a nested view.

Desired Outcome: enter image description here

my Design : enter image description here

As you can see I have problems allocating the text horizontally

Code:

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>
    );
  }
}

Any help is much appreciated, it looks super simple but I just cannot get it to work. Thank you!

Masuk Helal Anik
  • 2,155
  • 21
  • 29
Fredyonge
  • 300
  • 1
  • 4
  • 17

2 Answers2

1

It seems like you want to show the name on left side and price on right side of view. So you can try it this way,

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

Just mention your marginLeft: 'auto'. Also remove justifyContent: "space-between", from your parent view's style.

gprathour
  • 14,813
  • 5
  • 66
  • 90
0

Try to set your styles.cardBorder to have alignItems: 'center' and 'width': '100%'. I think your texts are centered in its container fine, but the container is not taking the full width. Maybe you only need 'width': '100%'.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
  • Hey ZenBoat! Thanks for you answer, unfortunately, it didn't work. I tried it on styles.Cardborder and on the container view of my . – Fredyonge Dec 21 '19 at 19:12