使用Webview作为自定义控件的动态选项卡

时间:2012-09-04 04:15:49

标签: android custom-controls android-webview android-tabhost

我在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);
    }
}

如果用户传递了网址,则应将其加载到特定网页视图how can i identify which tab is selected, and how to load the url in a corresponding webview中。

1 个答案:

答案 0 :(得分: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.

这是 为我工作

相关问题