[英]React Native Switch
react-native 中是否有一些方法可以创建具有这种精确设计的切换器?
我正在使用这个 React Native Switch,它运行良好,但我需要加上文本“on”和“off”,也许是自定义组件? https://reactnative.dev/docs/switch
谢谢!
你可以使用 React Native 创建自己的自定义组件。 只需继承它并创建自己的组件,例如https://github.com/arshigtx/react-native-custom-switch创建组件副本内部 src 修复问题,然后将其设为您自己的。 请启动项目。
----- 已编辑 ------
import React, { useState } from "react";
import { View, Switch, StyleSheet,Text } from "react-native";
const App = () => {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
return (
<View style={styles.container}>
<View>
{
isEnabled ? <Text style={{position: 'absolute', color: 'white', top: 3, left: 2, zIndex: 5, fontSize: 11}} >On</Text> : <Text style={{position: 'absolute', color: 'white', top: 3, left: 20, zIndex: 5, fontSize: 11}}>Off</Text>
}
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={isEnabled}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
}
});
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.