React Native android 按钮的波纹效果

TouchableNativeFeedback

A wrapper for making views respond properly to touches (Android only). On Android this component uses native state drawable to display touch Feedback. At the moment it only supports having a single View instance as a child node,as it's implemented by replacing that View with another instance of RCTView node with some additional properties set.

Background drawable of native Feedback touchable can be customized withbackgroundproperty.

Example:

renderButton : function ( ) { return ( <TouchableNativeFeedback onPress = { this ._onPressButton } background = {TouchableNativeFeedback . SelectableBackground( ) } > <View style = { {width : 150 , height : 100 , backgroundColor : 'red' } } > <Text style = { {margin : 30 } } >Button< /Text > < /View > < /TouchableNativeFeedback > ) ; } ,

Props

backgroundbackgroundPropType

Determines the type of background drawable that's going to be used to display Feedback. It takes an object withtypeproperty and extra data depending on thetype. It's recommended to use one of the static methods to generate that dictionary.

Methods

staticSelectableBackground(0)

Creates an object that represents android theme's default background for selectable elements (?android:attr/selectableItemBackground).

staticSelectableBackgroundBorderless(0)

Creates an object that represent android theme's default background for borderless selectable elements (?android:attr/selectableItemBackgroundBorderless). Available on android API level 21+.

staticRipple(color,borderless)

Creates an object that represents ripple drawable with specified color (as a string). If propertyborderlessevaluates to true the ripple will render outside of the view bounds (see native actionbar buttons as an example of that behavior). This background type is available on Android API level 21+.

Parameters:
Name and Type Description
color

string

The ripple color

borderless

boolean

If the ripple can render outside it's bounds




        <Button
                background={ (Platform.OS === 'android') ? TouchableNativeFeedback.Ripple('#f39c12',true) : null }
                style={ styles.buttonStyle }
                textStyle={ styles.textStyle }
                onPress={ () => {
                            console.log('world!')
                          } }>
          Hello
        </Button>

检测平台如果是Android平台,就设置波纹效果

background={ (Platform.OS === 'android') ? TouchableNativeFeedback.Ripple('#f39c12',true) : null }

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...