2012-09-04 29 views
0

我在android中創建自定義控件。該自定義控件應該帶有Webview選項卡。如果我從用戶端使用這個自定義控件,那麼他們可以訪問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中的用戶,那麼就應該在特定的web視圖加載,how can i identify which tab is selected, and how to load the url in a corresponding webview

回答

0

我已經解決了這個問題:-)

1. I have created 4 instances of WebView from user side class 
2. Passed this with url to create the tab in CustomTab control 
3. Inside the CustomTab, need to set the Content of Tab as this webview instance 
4. After creation of Tab inside the CustomTab, should load this passed Url. 

這是工作。