ReactNative組件的封裝

ReactNative組件的封裝

官網地址https://facebook.github.io/react-native/docs/native-components-android.html

封裝原生組件的步驟

1.創(chuàng)建一個ViewManager的子類。

2.實現createViewInstance方法。

3.導出視圖的屬性設置器:使用@ReactProp(或@ReactPropGroup)注解。

4.把這個視圖管理類注冊到應用程序包的createViewManagers里。

5.實現JavaScript模塊。

第一步、創(chuàng)建ViewManager


public class XNButtonManager extends SimpleViewManager<CommonButton> {
    @Override
    public String getName() {
        return "CommonButton"; //1.CommonButton為現有自定義控件,對應RN中注冊組件的名稱;
    }

    @Override
    protected CommonButton createViewInstance(ThemedReactContext reactContext) {
        return new CommonButton(reactContext);//2.需要實現該接口,返回自定義控件的實例對象;
    }

    @ReactProp(name = "type") //設置按鈕類型
    public void setType(CommonButton button, int type) {
        button.setButtonType(type);
        button.invalidate();
    }

    @ReactProp(name = "radius")
    public void setRadius(CommonButton button, float radius) {
        button.setRadius(radius);
        button.invalidate();
    }

    @ReactProp(name = "frameColor")
    public void setFrameColor(CommonButton button, String frameColor) {
        button.setButtonFrameColor(frameColor);
        button.invalidate();
    }

    @ReactProp(name = "fillColor")
    public void setFillColor(CommonButton button, String fillColor) {
        button.setButtonFillColor(fillColor);
        button.invalidate();
    }

    @ReactProp(name = "text")
    public void setText(CommonButton button,String text){
        button.setText(text);
        button.setGravity(Gravity.CENTER);
        button.invalidate();
    }

    @ReactProp(name = "textColor")
    public void setTextColor(CommonButton button,String textColor){
        button.setTextColor(Color.parseColor(textColor));
        button.invalidate();
    }

    @ReactProp(name ="frameWidth")
    public void setFrameWidth(CommonButton button,int frameWidth){
        button.setButtonFrameWidth(frameWidth);
        button.invalidate();
    }
}

第二步、實現createViewManagers接口方法

public class XNReactPackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Arrays.<ViewManager>asList(
                new XNButtonManager() //在這里添加自定義控件的Manager實例對象;
        );
    }
}

第三步、注冊ReactNative組件

在RN項目根目錄下新建CommonButton.js文件


'use strict'

import PropTypes from 'prop-types';
import { requireNativeComponent, View } from 'react-native';

//定義組件的屬性及類型
var CommonButton = {
    name : "CommonButton",
    propTypes : {
        type : PropTypes.number,
        frameColor: PropTypes.string,
        fillColor : PropTypes.string,
        radius : PropTypes.number,
        text : PropTypes.string,
        frameWidth: PropTypes.number,
        textColor : PropTypes.string,
        ...View.propTypes
    }
}
//導出組件
module.exports = requireNativeComponent("CommonButton",CommonButton);

第四步、在RN中使用導出的組件

import CommonButton from '../modlue/CommonButton';
... ...
export default class Welcome extends Component {

    constructor(props) {
        super(props);
    }

    render() {
        return (

            <View style={styles.container}>
                <CommonButton
                    type = { 1 }//1 fill,2: stroke
                    fillColor="#ff0000"
                    frameColor="#ff0000"
                    frameWidth={ 2 }
                    radius={ 15 }
                    text = "自定義按鈕"
                    textColor="#FFFFFF"
                    style = { styles.commonButton }
                />

                <Button
                    style={styles.button}
                    onPress={() => this.transfer()}
                    title="首頁"
                />
            </View>
        );
    }

const styles = StyleSheet.create({
    commonButton : {
        width:150,
        height:50
    }
});

AppRegistry.registerComponent('Welcome', () => Welcome);

image.png
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,094評論 25 709
  • 發(fā)現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,405評論 4 61
  • 學習ReactNative有一段時間了,也加了好多群,看見群里每天那么多人在那兒討論和學習ReactNative,...
    光無影閱讀 7,624評論 28 41
  • 監(jiān)督學習中,如果預測的變量是離散的,我們稱其為分類(如決策樹,支持向量機等),如果預測的變量是連續(xù)的,我們稱其為回...
    小灰灰besty閱讀 1,546評論 0 5
  • 林千禧漸漸習慣了嘔吐,每頓吃幾口,然后等惡心的感覺一點一點泛上來,拿上水杯跑去廁所。一開始是嘔吐,漱口,結束。。。...
    何青猊閱讀 151評論 0 0

友情鏈接更多精彩內容