我正在构建一个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一起滚动,但对于我的项目,我并不需要它。
发布于 2017-09-14 05:14:34
所以我自己花了一些时间,我的解决方案有多个部分:
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.layout_page);super.init();loadUrl(“like+ getFilesDir()+"index.html");....and其他你喜欢的代码...} @Override protected CordovaWebView makeWebView() { SystemWebViewEngine systemWebViewEngine = new SystemWebViewEngine((SystemWebView) findViewById(R.id.cordovaWebView));return new CordovaWebViewImpl(systemWebViewEngine);} @Override protected void createViews() { //必须重写此参数,否则将崩溃,如果(preferences.contains(" backgroundColor ")) { int BackgroundColor= preferences.getInteger("BackgroundColor",Color.BLACK);//活动背景:} appView.getView().requestFocusFromTouch();}
因此,makeWebView()是关键部分,您可以在其中覆盖CordovaActivity的方法,以便从XML返回您自己的CordovaWebView实例。
createViews()方法也必须被覆盖,因为如果检查CordovaActivity代码,就会发现有一个setContentView(appView.getView()),否则会导致崩溃。确保在覆盖时删除该部分。
最后是xml (不是整个内容):
<!-- The main content view -->
<org.apache.cordova.engine.SystemWebView
android:id="@+id/cordovaWebView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
发布于 2014-05-22 19:51:01
你有没有试着看看你的视图的顺序?也许它覆盖了你的网页视图
//隐藏webview
//webview.setVisibility(View.GONE);//测试新视图是否出现
Webview.setZOrderOnTop(真);
//显示新视图myView.setVisibility(View.VISIBLE);
myView.bringToFront();
我会假设你是100%确定这行得到一个有效的父级,你可以添加一个视图。
FrameLayout frameLayout = (FrameLayout) webView.getParent().getParent();
https://stackoverflow.com/questions/19372746
复制相似问题