如何在React Native上设置Slider的样式

时间:2019-02-18 14:48:53

标签: javascript reactjs react-native

我有一个特定的滑块。我该如何定型?我使用react-native-slider。这是我的代码(我从此example修改了代码):

    render() {
        const sliderStyle = {
            sliderDummy: {
                width: 200,
                height: 80,
                position: 'absolute',
            },
            sliderReal: {
                backgroundColor: colors.primary,
                width: (this.state.slideValue / 50) * 200,
                height: 80,
                borderBottomRightRadius: 100,
                borderTopRightRadius: 100
            }
        }
        return (
            <View style={styles.container}>

                <View style={{overflow: 'hidden'}}>
                    <View style={{flexDirection: 'row', position: 'absolute'}}>
                        <View style={sliderStyle.sliderDummy}></View>
                        <View style={sliderStyle.sliderReal}></View>
                    </View>
                <Slider
                    style={{width: 200, height: 80}}
                    minimumValue={0}
                    maximumValue={50}
                    value={this.state.slideValue}
                    onValueChange={(value)=> this.setState({ slideValue: value}) }
                    maximumTrackTintColor='transparent'
                    minimumTrackTintColor='transparent'
                    thumbStyle={styles.thumb}
                />
                    <Text>{this.state.slideValue}</Text>
                </View>
            </View>
        );
    }
}

当我向右滑动时,轨迹的中间有圆角:

enter image description here

但是我需要这样的东西:

enter image description here

我的错误在哪里?谢谢。

1 个答案:

答案 0 :(得分:0)

我解决这个问题的方法比预期的容易。我只是设置trackStyle(react-native-slider),这就像一个魅力。但是现在我还有另一个问题,如何获得像“斑马”一样的滑块样式。

render() {
        return (
            <View style={styles.container}>
                <Slider
                    style={{width: 150, height: 80}}
                    minimumValue={this.state.min}
                    maximumValue={this.state.max}
                    value={this.state.min}
                    onValueChange={(value)=> this.setState({ slideValue: value}) }
                    maximumTrackTintColor='transparent'
                    minimumTrackTintColor={colors.primary}
                    thumbStyle={styles.thumb}
                    trackStyle={styles.track}
                    step={1}
                />

                <Text>{this.state.slideValue}</Text>
                <Text>min {this.state.min}</Text>
                <Text>max {this.state.max}</Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
    },
    thumb: {
        width: 50,
        height: 80,
        backgroundColor: colors.primary,
        borderBottomRightRadius: 100,
        borderTopRightRadius: 100,

    },
    track:{
        height: 80,
        borderBottomRightRadius: 20,
        borderTopRightRadius: 20,
    }
});

enter image description here