【问题标题】:how to make touchablehighlight remain dark when tapped on?点击时如何使touchablehighlight保持黑暗?
【发布时间】:2019-12-02 08:38:46
【问题描述】:

如何使 touchablehighlight 按钮在点击时保持黑暗?当再次点击时,它们会变回原来的阴影。

【问题讨论】:

    标签: react-native react-native-ios


    【解决方案1】:

    通过透明度调整使其保持黑暗或明亮。如果你想改变颜色,你可以改变颜色。

      state={
        check : false,
        opacity : 0.2
      }
    
      onpress = () => {
        if (!this.state.check) {
          this.setState({
            opacity : 0.2,
            check: true
          })
        } else {
          this.setState({
            opacity : 1,
            check: false
          })
        }
      }
    
    <TouchableHighlight style={{alignItems:"center", backgroundColor: "black", opacity: this.state.opacity }} onPress={() => this.onpress()}>
      <Text style={{color: "#ffffff"}}>TouchableHighlight</Text>
    </TouchableHighlight>
    

    你可以使用多种内联样式

    <TouchableHighlight style={[styles.touchablehighlight ,{ opacity: this.state.opacity }]} onPress={() => this.onpress()}>
    

    【讨论】:

    • 你不能从样式表访问 this.state
    • @lilyfromtheblock 您可以使用多种内联样式。使用方法我附上了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多