2012-07-25 71 views
0

我有一个标签栏,我想要添加到多个活动。我有一个TabController.java它看起来像这样标签栏不显示在活动

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

public class TabController extends TabActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabcontroller); 

    TabHost tabHost = getTabHost();   
    Intent intent; 

    intent = new Intent().setClass(this, Help.class); 
    TabSpec specHelp = tabHost.newTabSpec("Help").setIndicator("Help") 
        .setContent(intent); 


    intent = new Intent().setClass(this, Services.class); 
    TabSpec specServices = tabHost.newTabSpec("Services").setIndicator("Services") 
        .setContent(intent); 


    intent = new Intent().setClass(this, Inbox.class); 
    TabSpec specInbox = tabHost.newTabSpec("Inbox").setIndicator("Inbox") 
        .setContent(intent); 


    intent = new Intent().setClass(this, About.class); 
    TabSpec specAbout = tabHost.newTabSpec("About").setIndicator("About") 
        .setContent(intent); 


    intent = new Intent().setClass(this, More.class); 
    TabSpec specMore = tabHost.newTabSpec("More").setIndicator("More") 
        .setContent(intent); 

    tabHost.addTab(specHelp); 
    tabHost.addTab(specServices); 
    tabHost.addTab(specInbox); 
    tabHost.addTab(specAbout);   
    tabHost.addTab(specMore); 
} 

tabcontroller.xml

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<TabHost android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@android:id/tabhost" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_alignParentBottom = "true" 
      android:layout_height="wrap_content" /> 

     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     </FrameLayout> 

    </RelativeLayout> 

    </TabHost> 
</merge> 

我想与做的是要为布局添加到多个活动。当我尝试添加布局时,我使用<include layout="@layout/tabcontroller"/>。当我运行项目时,我的选项卡栏不出现在屏幕上。

如何将此标签栏添加到我的活动中? PS。 TabController.java是不是我的主要活动

回答

0

尝试在本教程超过在考虑看看:http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android

一件事我知道,也许我错了,是根Activity类必须继承TAbActivity并为您的TabController。 java不是主要活动。

编辑:这是怎么我得到标签这是不是我的根活动:

公共类ListFragment扩展片段实现OnTabChangeListener {

View v; 
TabHost tabHost;  

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    v = inflater.inflate(R.layout.main_menu_libraries, container, false); 


    tabHost=(TabHost) v.findViewById(R.id.tabHost); 
    tabHost.setup(); 

    TabSpec spec1=tabHost.newTabSpec("Tab 1"); 
    spec1.setContent(R.id.tab1); 
    spec1.setIndicator("About",getResources().getDrawable(R.drawable.android)); 

    TabSpec spec2=tabHost.newTabSpec("Tab 2"); 
    spec2.setContent(R.id.tab2); 
    spec2.setIndicator("Blog",getResources().getDrawable(R.drawable.android)); 

    TabSpec spec3=tabHost.newTabSpec("Tab 3"); 
    spec3.setIndicator("Donate",getResources().getDrawable(R.drawable.android)); 
    spec3.setContent(R.id.tab3); 

    TabSpec spec4=tabHost.newTabSpec("Tab 4"); 
    spec4.setIndicator("Explore",getResources().getDrawable(R.drawable.android)); 
    spec4.setContent(R.id.tab4); 

    tabHost.addTab(spec1); 
    tabHost.addTab(spec2); 
    tabHost.addTab(spec3); 
    tabHost.addTab(spec4); 

    return v; 
} 

正如上面的代码中,我使用的片段。所以我使用包含选项卡和tabwidgets的menu_libraries.xml来膨胀我的VIew。 在OncreateView函数内,我还定义和扩展了选项卡。 不是最好的答案,但我想你可以尝试。

+0

我的HomeScreen是我的主页,没有标签栏,所以我有它扩展活动。如果我使'TabController'成为根活动,我怎么能让我的'HomeScreen'首先显示并且不显示标签栏? – BigT 2012-07-25 16:02:10

+0

所以我使用片段,并在我的应用程序,我甚至没有扩展TabActivity类。我编辑了我以前的评论,以便您可以检查 – Android2390 2012-07-25 16:06:12

+0

该方法是否进入我需要标签栏的每个活动? – BigT 2012-07-25 16:33:50