我将如下图所示在Android 4.0中开发一个android应用。



我必须动态创建这些选项卡,然后动态调用addTab()方法并动态创建选项卡栏。将ActionBarWithTab满足我的要求吗?将我引导至正确的方向。请提供教程。

编辑1:我需要在ActionBar中创建的单独的标签栏中加载Webview。

EIDT 2:为了使用TabHost创建选项卡栏,首先,我为TabHost创建了ViewGroup f并添加了选项卡栏项,如下面的代码。在选项卡栏项目中,我创建了布局,在布局内创建了一个webview,并将动态网址加载到了选项卡栏项中。最后,我将视图组设置为contentview();。

    sTabHost = new TabHost(context,null);
    sTabHost.setLayoutParams(
            new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


    TabWidget tabWidget = new TabWidget(context);
    tabWidget.setId(android.R.id.tabs);
    sTabHost.addView(tabWidget, new LinearLayout.LayoutParams(
              LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


    FrameLayout frameLayout = new FrameLayout(context);
    frameLayout.setId(android.R.id.tabcontent);
    final float scale = context.getResources().getDisplayMetrics().density;
    int paddingtop = (int) (64 * scale + 0.5f);
    frameLayout.setPadding(0, paddingtop, 0, 0);
    sTabHost.addView(frameLayout, new LinearLayout.LayoutParams(
              LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    sTabHost.setup();


然后,我添加了如下所示的选项卡项。

 public void addTabItem(final String url, String tabTitle, Drawable tabIcon)
{
    TabSpec ts1 = sTabHost.newTabSpec(tabTitle);
    if(tabTitle.equals(""))
    {
        int childcount=sTabHost.getChildCount();
        tabTitle="Tab" + String.valueOf(childcount+1);
    }
    if(tabIcon==null)
        ts1.setIndicator(tabTitle);
    else
        ts1.setIndicator(tabTitle,tabIcon);
    ts1.setContent(new TabHost.TabContentFactory(){
         public View createTabContent(String tag)
         {
                LinearLayout panel = new LinearLayout(sActiveContext);
                panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                panel.setOrientation(LinearLayout.VERTICAL);
                FrameLayout layout=new FrameLayout();
                    // Here I am creating the webview loaded with the url within the layout and placed into the above FrameLayout.
                panel.addView(layout);
                return panel;
         }
    });
    sTabHost.addTab(ts1);
    sTabHost.setOnTabChangedListener(this);
  }


现在,如何像添加的图像一样在操作栏中实现此目的?

我创建了带有以下代码标签的操作栏。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tabA = actionBar.newTab();
    tabA.setText("Tab A");
    tabA.setTabListener(new TabListener<MyFragmentA>(this, "Tag A", MyFragmentA.class));
    actionBar.addTab(tabA);
}


TabListener类如下。

public static class TabListener<T extends Fragment>
    implements ActionBar.TabListener{

    private final Activity myActivity;
    private final String myTag;
    private final Class<T> myClass;

    public TabListener(Activity activity, String tag, Class<T> cls) {
        myActivity = activity;
        myTag = tag;
        myClass = cls;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        // Check if the fragment is already initialized
        if (myFragment == null) {
            // If not, instantiate and add it to the activity
            myFragment = Fragment.instantiate(myActivity, myClass.getName());
            ft.add(android.R.id.content, myFragment, myTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.show(myFragment);
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        if (myFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.hide(myFragment);
        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }

}


Fragment类如下。

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_c, container, false);

    WebView webview = (WebView) myFragmentView.findViewById(R.id.webview);
    webview.setWebViewClient(new MyWebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setPluginsEnabled(true);
    webview.getSettings().setBuiltInZoomControls(false);
    webview.getSettings().setSupportZoom(false);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.loadUrl("http://jquerymobile.com/demos/1.2.1/");

    return myFragmentView;
}

public class MyWebViewClient extends WebViewClient {
    /* (non-Java doc)
     * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
     */


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".mp4"))
        {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "video/*");

            view.getContext().startActivity(intent);
            return true;
        }
        else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
}


在以上实现中,我创建了布局和单独的片段类。而不是像这样创建,如何在不创建xml布局和单独的片段类的情况下实现这一点。就像我为TabHost标签栏发布的代码一样。

编辑3:我已经创建了操作栏和选项卡项,如下面的代码。

 public void addTabBar(Context context)
{
    sActiveContext=context;
    sActionBar = getActionBar();
    sActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}

public void addTabItem(final String url, String tabTitle)
{
    Tab tab = sActionBar.newTab();
    if(tabTitle.equals(""))
    {
        int childcount=sActionBar.getTabCount();
        tabTitle="Tab" + String.valueOf(childcount+1);
    }
    tab.setText(tabTitle);
     //Here I need to create the Layout with the webview and loaded into the created tab.
    tab.setTabListener(this);
    sActionBar.addTab(tab);
}


如何实现将具有给定URL的Webview加载到特定标签。

最佳答案

编辑答案3:-
尝试这个,

@SuppressLint("ValidFragment")
public class Sample extends FragmentActivity implements TabListener{
    private ActionBar mActionBar=null;
    private ArrayList<String> arrayList=null;
    private LinearLayout linearLayout=null;
    private WebView webView=null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        linearLayout=new LinearLayout(this);
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);
        mActionBar=getActionBar();
        mActionBar.setDisplayShowHomeEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            webView=new WebView(this);
        linearLayout.addView(webView);
        arrayList=new ArrayList<String>();
        addTab(0,"https://docs.google.com/viewer?url=http://121.242.120.82:8090/iSmartDM/DC/KIMS_Hospital_28/KIMS_hospital_Default_29" +
                    "/Physician_2_37/DOC/stamos-5644-00@gs138a-[01062006]-[160859]-[ds4000]-[].rtf");
        addTab(1,"https://www.google.co.in");
        addTab(2,"http://stackoverflow.com/questions/15964641/tab-bar-app-in-android-4-0/16009038?noredirect=1#comment22832140_16009038");
    }
    private void addTab(int position,String URL)
    {
mActionBar.addTab(mActionBar.newTab().setTag("tab"+String.valueOf(position)).
                    setText("Tab "+String.valueOf(position)).setTabListener(this));
            arrayList.add(URL);
        }
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
    webView.getSettings().setJavaScriptEnabled(true);
                for (int i = 0; i < arrayList.size(); i++) {
                    if(tab.getPosition()==i)
                    {
                        webView.loadUrl(arrayList.get(i));
                        System.out.println(arrayList.get(i));
                        break;
                    }
                }
        }
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            //if(webView!=null)
                //linearLayout.removeView(webView);
        }
    }

10-08 07:25
查看更多