android reactnative 嵌入原生页面中

在xml中加入ReactView组件

<com.facebook.react.ReactRootView
        android:id="@+id/test_js"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv_tv"/>

在activity中添加代码

private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;

    public void initData() {
        mReactRootView = (ReactRootView) mLayoutView.findViewById(R.id.test_js);
        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(((Activity)getContext()).getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSBundleFile(bundlePath)
                .setJSMainModulePath("index.android")//对应index.android.js
                .addPackage(new MainReactPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG) //开发者支持,BuildConfig.DEBUG的值默认是false,无法使用开发者菜单
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
//        这里的ReactNativeView对应index.android.js中AppRegistry.registerComponent('ReactNativeView', () => ReactNativeView)的ReactNativeView
        mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", null);
    }

由上面的代码可知,ReactRootView 是从view中find到的,而不是new出来的,起ReactInstanceManager也是自己build出来的。
其本质上mReactRootView 就是一个view组件,与ReactActivity中将setContentView(mReactRootView);一个原理

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值