2015-01-16 12 views
5

मैं अपने ऐप पर TabHost का उपयोग करने की कोशिश कर रहा हूं, और मैंने इसे डिज़ाइन का उपयोग करके अपनी गतिविधि में खींच लिया है, लेकिन जब मैं इसे चलाता हूं, तो यह दिखाई नहीं देगा, बस सफेद स्क्रीन प्राप्त करें क्या कोई जानता है क्यों?स्क्रीन में टैबहोस्ट नहीं दिख रहा है

<TabHost 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/tabHost" 
    android:layout_gravity="center_horizontal"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

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

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

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"></LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"></LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"></LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

उत्तर

9

यह बस क्योंकि तुम सिर्फ TabHost केवल XML कोड का उपयोग नहीं बना सकते हो रहा है। आप TabHost इस तरह के TabSpec रों जोड़ने की जरूरत:

TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost); 

TabSpec tab1 = tabHost.newTabSpec("First Tab"); 
TabSpec tab2 = tabHost.newTabSpec("Second Tab"); 
TabSpec tab3 = tabHost.newTabSpec("Third Tab"); 

tab1.setIndicator("Tab1"); 
tab1.setContent(new Intent(this,TabActivity1.class)); 

tab2.setIndicator("Tab2"); 
tab2.setContent(new Intent(this,TabActivity2.class)); 

tab3.setIndicator("Tab3"); 
tab3.setContent(new Intent(this,TabActivity3.class)); 

tabHost.addTab(tab1); 
tabHost.addTab(tab2); 
tabHost.addTab(tab3); 
+1

क्या "setContent (नई आशय (यह, TabActivity2.class))" विधि के लिए प्रयोग किया जाता है? मैं इसे वही कर रहा था लेकिन ऐप एक नलपॉन्टर एक्सेप्शन फेंक रहा था, इसलिए मैंने इसे सेटकंटेंट (R.id.yourTabId) पर बदल दिया; – Sandoval0992

+0

इस उदाहरण में मैं टैब में गतिविधि डाल रहा हूं ताकि आपको अपनी प्रोजेक्ट – SMR

+0

में 'टैबएक्टिविटी 1,2,3'' बनाना होगा। आप tabHost.setup() भूल गए हैं (मेरे मामले में अपवाद था) –

संबंधित मुद्दे