我将在 Android 4.0 中开发一个 android 应用程序,如下图所示。
我必须动态创建这些选项卡,动态调用 addTab() 方法并动态创建选项卡栏。ActionBarWithTab是否适合我的要求?引导我走向正确的道路。请提供教程。
编辑 1:我需要在 ActionBar 中创建的单独选项卡栏中加载 webview。
EIDT 2:为了使用 TabHost 创建标签栏,首先我为 TabHost 创建了 ViewGroup f,并添加了标签栏项目,如下面的代码。在标签栏项目中,我创建了布局,在布局内有一个 webview 并将动态 url 加载到标签栏项目中。最后我将视图组设置为 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
}
}
片段类如下。
@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 加载到特定选项卡。