Flex

flex.tsx

  1. import React from "react";
  2. import { StyleSheet, Text, View } from "react-native";
  3. const Flex = () => {
  4. return (
  5. <View style={[styles.container, {
  6. // Try setting `flexDirection` to `"row"`.
  7. flexDirection: "column"
  8. }]}>
  9. <View style={{ flex: 1, backgroundColor: "red" }} />
  10. <View style={{ flex: 2, backgroundColor: "darkorange" }} />
  11. <View style={{ flex: 3, backgroundColor: "green" }} />
  12. </View>
  13. );
  14. };
  15. const styles = StyleSheet.create({
  16. container: {
  17. flex: 1,
  18. padding: 20,
  19. },
  20. });
  21. export default Flex;

app.tsx

  1. import React from 'react'
  2. import Flex from './Flex';
  3. const App = () => {
  4. return (
  5. <Flex />
  6. );
  7. }
  8. export default App;

Flex Direction

FlexDirectionBasics.tsx

  1. import React, { useState } from "react";
  2. import { StyleSheet, Text, TouchableOpacity, View } from "react-native";
  3. const FlexDirectionBasics = () => {
  4. const [flexDirection, setflexDirection] = useState("column");
  5. return (
  6. <PreviewLayout
  7. label="flexDirection"
  8. values={["column", "row", "row-reverse", "column-reverse"]}
  9. selectedValue={flexDirection}
  10. setSelectedValue={setflexDirection}
  11. >
  12. <View
  13. style={[styles.box, { backgroundColor: "powderblue" }]}
  14. />
  15. <View
  16. style={[styles.box, { backgroundColor: "skyblue" }]}
  17. />
  18. <View
  19. style={[styles.box, { backgroundColor: "steelblue" }]}
  20. />
  21. </PreviewLayout>
  22. );
  23. };
  24. const PreviewLayout = ({
  25. label,
  26. children,
  27. values,
  28. selectedValue,
  29. setSelectedValue,
  30. }) => (
  31. <View style={{ padding: 10, flex: 1 }}>
  32. <Text style={styles.label}>{label}</Text>
  33. <View style={styles.row}>
  34. {values.map((value: string) => (
  35. <TouchableOpacity
  36. key={value}
  37. onPress={() => setSelectedValue(value)}
  38. style={[
  39. styles.button,
  40. selectedValue === value && styles.selected,
  41. ]}
  42. >
  43. <Text
  44. style={[
  45. styles.buttonLabel,
  46. selectedValue === value && styles.selectedLabel,
  47. ]}
  48. >
  49. {value}
  50. </Text>
  51. </TouchableOpacity>
  52. ))}
  53. </View>
  54. <View style={[styles.container, { [label]: selectedValue }]}>
  55. {children}
  56. </View>
  57. </View>
  58. );
  59. const styles = StyleSheet.create({
  60. container: {
  61. flex: 1,
  62. marginTop: 8,
  63. backgroundColor: "aliceblue",
  64. },
  65. box: {
  66. width: 50,
  67. height: 50,
  68. },
  69. row: {
  70. flexDirection: "row",
  71. flexWrap: "wrap",
  72. },
  73. button: {
  74. paddingHorizontal: 8,
  75. paddingVertical: 6,
  76. borderRadius: 4,
  77. backgroundColor: "oldlace",
  78. alignSelf: "flex-start",
  79. marginHorizontal: "1%",
  80. marginBottom: 6,
  81. minWidth: "48%",
  82. textAlign: "center",
  83. },
  84. selected: {
  85. backgroundColor: "coral",
  86. borderWidth: 0,
  87. },
  88. buttonLabel: {
  89. fontSize: 12,
  90. fontWeight: "500",
  91. color: "coral",
  92. },
  93. selectedLabel: {
  94. color: "white",
  95. },
  96. label: {
  97. textAlign: "center",
  98. marginBottom: 10,
  99. fontSize: 24,
  100. },
  101. });
  102. export default FlexDirectionBasics;

注意看自定义组件的写法,以及 useState 的用法。

  1. import React from 'react'
  2. import FlexDirectionBasics from './FlexDirectionBasics';
  3. const App = () => {
  4. return (
  5. <FlexDirectionBasics />
  6. );
  7. }
  8. export default App;

其他的

https://reactnative.dev/docs/flexbox