带有原生控件的Android PhoneGap

时间:2011-09-06 13:42:43

标签: android layout webview cordova

我正在尝试使用PhoneGap构建一个Android应用程序。

我需要能够使用PhoneGap WebView(super.appView)及其所有javascript魔法,但我还需要在WebView周围显示一些本机UI控件。

这篇文章部分提供了解决方案Android PhoneGap Plugin, UI tabbar, resize WebView

有没有人设法使用原生UI实现PhoneGap?

我也将使用GestureOverlayView,但这是另一个故事;)

3 个答案:

答案 0 :(得分:12)

答案:

super.onCreate(savedInstanceState);
//creates super.appView and calls setContentView(root) in DroidGap.java
init();
//just an empty LinearLayout
layoutId = R.layout.blank;
view = new LinearLayout(this);
setContentView(layoutId);
view.addView(your_component_here);
view.addView((View) appView.getParent()); //adds the PhoneGap browser at index 1
//accesses the browser at index 1. Tells browser to not fill view
view.getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
setContentView(view);

我很难告诉你它是如何工作的,我只能说它确实如此,而且这都是我自己的工作。

将视图设置为其他颜色可以帮助您查看正在发生的事情......

view.setBackgroundColor(Color.BLUE);

使用PhoneGap-1.0.0.jar到目前为止的最新版本。

答案 1 :(得分:8)

更清洁的方法:

super.onCreate(savedInstanceState);

// Create native view with UI controls.
View header = View.inflate(getContext(), R.layout.header, null);

// PhoneGaps WebView is located inside a LinearLayout.
// The protected (and therefore inherited) variable root
// references this LinearLayout. Add your native Views
// to this variable.
root.addView(header);

// Create WebView and add it automatically to the LinearLayout.
super.loadUrl("file:///android_asset/www/index.html");

答案 2 :(得分:0)

是的,您可以使用插件在HTML中嵌入本机控件。

使用插件从HTML页面调用包含本机视图的本机方法。

e.g。 window.plugins.anatomyPlugin.showAudio();

我用它来显示本机的音频播放器设计。

来自PhoneGap的guide可能会有所帮助。