在React Native中进行主题和样式的管理可以使用第三方库(如styled-components、react-native-paper等)或自定义方案。以下是自定义方案的基本步骤:
const theme = {
colors: {
primary: '#007aff',
background: '#f8f8f8',
text: '#333333',
},
fontSizes: {
small: 12,
medium: 16,
large: 20,
},
};import { ThemeContext } from './ThemeContext';
const { colors, fontSizes } = useContext(ThemeContext);const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.background,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: fontSizes.large,
color: colors.text,
},
});<View style={styles.container}>
<Text style={styles.text}>Hello, world!</Text>
</View>