波纹效果在拐角处泄漏,好像可按下按钮具有边框半径

时间:2020-07-23 06:29:15

标签: reactjs react-native react-native-android react-native-ios react-component

在引用此文档pressable docs

后,我正在将Pressable用于按钮

现在,我想向按钮添加波纹效果,但是它无法正常工作。

      <Pressable
        android_ripple={{color: 'red', borderless: false}}
        style={{backgroundColor: 'blue',borderRadius : 10}}>
        <Text style={{alignSelf: 'center'}}>Button</Text>
      </Pressable>

如果按钮具有半径,则波纹效果没有边界半径。 波纹效果角超出弯曲半径看起来很尴尬。

3 个答案:

答案 0 :(得分:7)

您可以将pressable包装到View中,然后将borderRadius:10传递给overflow,并将overflow:'hidden'隐藏为View样式。

<View style={{ borderRadius: 10, overflow: 'hidden' }}>
    <Pressable
      android_ripple={{ color: 'red', borderless: false, }}
      style={{ backgroundColor: 'blue', borderRadius: 10 }}>
      <Text style={{ alignSelf: 'center' }}>Button</Text>
    </Pressable>
  </View>

答案 1 :(得分:3)

对我没有任何帮助,所以我自己解决了这个问题。

  • pressable应该包装在视图中
  • 视图必须具有不填充的边距
  • 边界半径必须处于可见状态,不能按下
  • 可压缩组件必须具有填充而不是空白
  • 然后通过 android_ripple = {{color:'black',borderless:true}} 将波纹添加到可压制。
<View style={styles.buttonView}>
              <Pressable
                onPress={() => {}}
                android_ripple={{color: 'black', borderless: true}}
                style={styles.loginButton}>
                <Text style={styles.buttonText}>Login</Text>
              </Pressable>
            </View>
buttonView: {
    alignSelf: 'stretch',
    justifyContent: 'center',
    borderRadius: 10,
    elevation: 25,
    margin: 10,
  },
  loginButton: {
    height: 50,
    backgroundColor: '#0f4c75',
    padding: 10,
    alignItems: 'center',
    justifyContent: 'center',
  },
  buttonText: {
    color: 'white',
    fontSize: 16,
    textTransform: 'uppercase',
    fontFamily: 'sans-serif-light',
  },

答案 2 :(得分:0)

事实证明,另一个解决方案并未涵盖所有边缘情况……我需要将视图包装 2 次才能在 iOS 上管理 elevationshadow。 (按钮也处于绝对位置)。

export const PNFloatingButton = ({ children, ...props }) => {
    return (
        <View style={thisStyles.shadow}>
            <View style={thisStyles.parentContainer}>
                <Pressable style={thisStyles.button}
                           android_ripple={{ borderless: false, color: "#0F0" }}>
                    <Text>Button</Text>
                </PNPressable>
            </View>
        </View>
    )
}

const thisStyles = StyleSheet.create({
    shadow: {
        shadowColor: '#000',
        shadowOffset: { width: 0, height: 2 },
        shadowOpacity: 0.4,
        shadowRadius: 4,
        width: 56,
        height: 56,
        position: 'absolute',
        elevation: 2,
        right: 0,
        bottom: 0,
    },
    button: {
        padding: 4,
        backgroundColor: "#F00",
    },
    parentContainer: {
        // Pressable has issues with borderRadius on the ripple on android
        overflow: "hidden",
        borderRadius: 40,
    },
})