我正在构建一个 phonegap 插件,它需要在 PhoneGap 提供的 WebView 之上呈现本机 UI 视图。在iOS 中这很简单,只需创建视图并将其添加到PhoneGap 的webView 的scrollView 中。这将在 webView 顶部呈现控件并允许它随着 HTML 内容滚动(请注意,此示例使用 UIButton,但我将其应用于自定义 UI 控件):
-(void)createNativeControl:(CDVInvokedUrlCommand *)command
{
NSDictionary* options = [command.arguments objectAtIndex:0];
NSNumber* x = [options objectForKey:@"x"];
NSNumber* y = [options objectForKey:@"y"];
NSNumber* width = [options objectForKey:@"width"];
NSNumber* height = [options objectForKey:@"height"];
CGRect rect = CGRectMake([x floatValue], [y floatValue], [width floatValue], [height floatValue]);
self._nativeControl = [UIButton buttonWithType:UIButtonTypeSystem];
self._nativeControl.frame = rect;
[self._nativeControl addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self._nativeControl setTitle:@"Click me" forState:UIControlStateNormal];
[self.webView.scrollView addSubview:self._nativeControl];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackID];
}
我曾尝试在 Android 中做一些大致相同的事情,但没有成功。这是我最近的尝试:
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
System.out.println(String.format("%s action called", action));
if ("echo".equals(action)) {
String message = args.optString(0);
this.echo(message, callbackContext);
return true;
} else if ("createNativeControl".equals(action)) {
this.createNativeControl(callbackContext);
return true;
}
return false;
}
private void createNativeControl(CallbackContext callbackContext) {
// Find the frame layout parent and place the control on top - theoretically
// this should appear on TOP of the webView content since the TextView child is
// added later
FrameLayout frameLayout = (FrameLayout) webView.getParent().getParent();
TextView view = new TextView(frameLayout.getContext());
view.setText("Hello, Android!");
view.setVisibility(View.VISIBLE);
frameLayout.addView(view, 100,100);
callbackContext.success();
}
我怎样才能在 Android 中做到这一点?