0

我正在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.

4

1 回答 1

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.

对我有用

于 2012-09-07T04:15:46.047 回答