因此,我试图改变配色方案使用外观或useColorScheme,但我只能得到当前的主题。怎么改呢?
发布于 2022-09-14 08:30:42
您可以通过创建一个类似于customHook的
// hooks/useThemeColors.ts
import { useColorScheme } from 'react-native';
const Colors = {
light: {
background: "white",
text: "black",
},
dark: {
background: "black",
text: "white",
},
}
const useThemeColors = () => {
const colorScheme = useColorScheme()
const colors = Colors[colorScheme]
return colors
}
export default useThemeColors现在像这样在你的作品中使用它
从“from”导入(视图、文本、ViewStyle、TextStyle、StyleSheet }从“React -本机”导入react
从"hooks/ useThemeColors“导入useThemeColors
const App = () => {
const colors = useThemeColors()
const viewStyles: ViewStyle[] = [
styles.container,
{ backgroundColor: colors.background },
]
const textStyles: TextStyle[] = [styles.text, { color: colors.text }]
return (
<View style={viewStyles}>
<Text style={textStyles}>Hello, world!</Text>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
text: {
fontWeight: "bold",
fontSize: 20,
},
})
export default App希望能帮上忙。有疑问就放心吧
全归功于https://www.reactnativeschool.com/how-to-detect-user-color-preference-in-react-native
https://stackoverflow.com/questions/73713606
复制相似问题