【发布时间】:2016-09-21 10:04:38
【问题描述】:
如何在 React Native 中改变 switch 组件的大小?
<Switch onValueChange={this._changeReciveNotice.bind(this)}
value={this.state.isReciveNotice}
style={{width:20,height:10}}/>
此样式代码不起作用
【问题讨论】:
标签: react-native
如何在 React Native 中改变 switch 组件的大小?
<Switch onValueChange={this._changeReciveNotice.bind(this)}
value={this.state.isReciveNotice}
style={{width:20,height:10}}/>
此样式代码不起作用
【问题讨论】:
标签: react-native
您可以使用样式中的 transform 属性来调整开关的大小,
<Switch value={true}
style={{ transform: [{ scaleX: .8 }, { scaleY: .8 }] }}
onValueChange={(value) => {}} />
为了获得最佳效果,还要根据屏幕尺寸确定缩放值。
【讨论】:
为了扩展已经说过的内容,您可以通过以下方式处理屏幕尺寸:
import { moderateScale } from 'react-native-size-matters';
...
<Switch
style={{ transform: [{ scaleX: moderateScale(1, 0.2) }, { scaleY:
moderateScale(1, 0.2) }] }} />
【讨论】:
如果尺寸只是 UI 问题,您可以添加负边距
例如,在我的情况下,我会降低高度(一点点)以保持对齐:
<Switch style={{marginVertical: -8}} />
【讨论】:
<View
style={{
backgroundColor: this.state.value
? "rgba(81, 195, 157, 0.16)"
: "rgba(204, 204, 204, 0.16)",
borderRadius: 50,
paddingVertical: 2,
paddingHorizontal: 4
}}
>
<Switch
tintColor="transparent"
thumbTintColor={this.state.value ? "#51c39d" : "#cccccc"}
onTintColor="transparent"
value={this.state.value}
style={{ transform: [{ scaleX: 1.3 }, { scaleY: 1.3 }] }}
onValueChange={value => this.setState({ value })}
/>
</View>
【讨论】:
您必须更改状态值。
示例:
<Switch
onValueChange={(value) => this.setState({isReciveNotice: value})}
style={{marginBottom: 10}}
value={this.state.isReciveNotice} />
我认为您不能对 Switch 组件进行样式化。
【讨论】: