我正在android中创建自定义控件。该自定义控件应该具有带有 Webview 的选项卡。如果我从用户端使用了这个自定义控件,那么他们可以访问 webview 事件。我尝试了以下方法,
CustomTab.java
public class CustomTab extends Activity
{
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View m_vForm;
m_vForm = _createTABForm();
addTab("Tab1","Tab1");
addTab("Tab2","Tab2");
addTab("Tab3","Tab3");
addTab("Tab4","Tab4");
setContentView(m_vForm);
}
/** Create the TAB interface */
private ViewGroup _createTABForm() {
// construct the TAB Host
tabHost = new TabHost(this);
tabHost.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// the tabhost needs a tabwidget, that is a container for the visible tabs
TabWidget tabWidget = new TabWidget(this);
tabWidget.setId(android.R.id.tabs);
tabHost.addView(tabWidget, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// the tabhost needs a frame layout for the views associated with each visible tab
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setId(android.R.id.tabcontent);
frameLayout.setPadding(0, 65, 0, 0);
tabHost.addView(frameLayout, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tabHost.setup();
return tabHost;
}
public void addTab(String tagid, String title)
{
// create the tabs
TabSpec ts1 = tabHost.newTabSpec(tagid);
ts1.setIndicator(title);
ts1.setContent(new TabHost.TabContentFactory(){
public View createTabContent(String tag)
{
LinearLayout panel = new LinearLayout(CustomTab.this);
panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
panel.setOrientation(LinearLayout.VERTICAL);
WebView webview=new WebView(CustomTab.this);
webview.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
panel.addView(webview);
return panel;
}
});
tabHost.addTab(ts1);
}
}
如果用户传递了 url,那么它应该被加载到特定的 webview 中,how can i identify which tab is selected, and how to load the url in a corresponding webview
.