我正在构建一个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中做到这一点?
发布于 2015-07-14 20:04:23
使用Cordova Android 4.0.2,我可以在webview上添加一个视图,如下所示:
public class HVWMaps extends CordovaPlugin {
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
FrameLayout layout = (FrameLayout) webView.getView().getParent();
TextView textView = new TextView(layout.getContext());
textView.setBackgroundColor(Color.BLUE);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(500, 500);
params.setMargins(100, 100, 100, 100);
textView.setLayoutParams(params);
layout.addView(textView);
}
}
似乎CordovaWebView
的界面最近发生了变化,因此这可能不适用于旧版本。对于本例,您还需要将<param name="onload" value="true" />
添加到plugin.xml中的功能,以便调用initialize(..)
:
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="YourPlugin" >
<param name="android-package" value="com.your.plugin"/>
<param name="onload" value="true" />
</feature>
</config-file>
</platform>
这并不像你提到的iOS那样与webview一起滚动,但对于我的项目,我并不需要它。
https://stackoverflow.com/questions/19372746
复制相似问题