在React Native中进行开发构建时,您可以摇动设备或使用菜单按钮调出开发人员菜单.如何在此菜单中添加其他自定义项?我还没有找到任何关于添加其他项目的文档,但我认为从开发菜单中说服务器环境(dev,prod等)之间切换非常方便,而不是针对每个环境进行单独的构建测试. 最佳答案 DevSupportManager类可以为您执行此操作.您可以通过其ReactInstanceManager从ReactNativeHost实例获取它:
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
DevSupportManager devSupportManager = reactInstanceManager.getDevSupportManager();
有了这个,添加自定义开发选项如下:
devSupportManager.addCustomDevOption("Custom dev option", new DevOptionHandler() {
@Override
public void onOptionSelected() {
Toast.makeText(MainActivity.this, "Hello from custom dev option", Toast.LENGTH_SHORT).show();
}
});
您首先获得ReactNativeHost实例的位置取决于您的应用程序结构.如果您使用react-native init创建了应用程序,那么您的主应用程序类将包含以下内容:
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
……你可以打电话.
I have a complete sample here;看看MainActivity.OnCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainApplication application = (MainApplication) getApplication();
ReactNativeHost reactNativeHost = application.getReactNativeHost();
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
DevSupportManager devSupportManager = reactInstanceManager.getDevSupportManager();
devSupportManager.addCustomDevOption("Custom dev option", new DevOptionHandler() {
@Override
public void onOptionSelected() {
Toast.makeText(MainActivity.this, "Hello from custom dev option", Toast.LENGTH_SHORT).show();
}
});
}