首页 > 解决方案 > React Native 0.61 按钮样式的新方法是什么

问题描述

这篇旧帖子谈到titleStyletextStyle似乎不再基于 Button 道具:

在此处输入图像描述

如何将样式对象添加到按钮?现在我应该使用一些触摸功能来自定义视图吗?我需要实现:

在此处输入图像描述

谢谢。

标签: react-native

解决方案


我刚刚检查了 react native Buttonprops,如果您不复制源代码并将其制作成您自己的组件,则无法为其添加样式,您可以在这里找到它node_modules/react-native/Libraries/Components/Button.js

我建议你使用TouchableOpacity,这样你就可以做任何你想做的事情,如果没有必要使用Button. 像这样:

      <TouchableOpacity
        disabled={disabled || disabledNoLoading}
        style={[styles.buttonView, {
          backgroundColor: disabled || disabledNoLoading ? disabledColor : backgroundColor,
          width: buttonWidth,
          marginTop,
          borderColor
        }]}
        onPress={() => {Alert.alert('Pressed')}}
      >
        {
          disabled ? 
           (<Text>Loading...</Text>) 
           : (<Text style={[styles.buttonText, {color: textColor}]}>{text}</Text>)
        }
      </TouchableOpacity>

推荐阅读