如何在Android中的PhoneGap Cordova WebView上叠加本机视图?

时间:2013-10-15 03:19:54

标签: android cordova phonegap-plugins

我正在构建一个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中完成此操作?

3 个答案:

答案 0 :(得分:9)

使用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,但对于我的项目,我并不需要它。

答案 1 :(得分:0)

所以我自己花了一些时间,我的解决方案有多个部分:

  • 您的布局XML中有CordovaWebView
  • 您的活动扩展了CordovaActivity
  • 您可以覆盖几种方法:

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_page);
    super.init();
    loadUrl("file://" + getFilesDir()+"index.html");
    
    ....and other code you like...
    }
    
    
     @Override
        protected CordovaWebView makeWebView() {
            SystemWebViewEngine systemWebViewEngine =
            new SystemWebViewEngine((SystemWebView) findViewById(R.id.cordovaWebView));
    return new CordovaWebViewImpl(systemWebViewEngine);
        }
    
        @Override
        protected void createViews() {
    //must override this, otherwise crash
    if (preferences.contains("BackgroundColor")) {
        int backgroundColor = preferences.getInteger("BackgroundColor", Color.BLACK);
        // Background of activity:
        appView.getView().setBackgroundColor(backgroundColor);
    }
            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" />

答案 2 :(得分:-1)

您是否尝试过查看观看顺序? 也许它与你的webview重叠了

  

//隐藏webview
  //webview.setVisibility(View.GONE); //来测试是否   出现新视图   webview.setZOrderOnTop(真);

     

//显示新视图myView.setVisibility(View.VISIBLE);

     

myView.bringToFront();

我将假设您100%确定此行获得了一个有效的父级,您可以添加视图。

  

FrameLayout frameLayout =(FrameLayout)   。webView.getParent()的getParent();