2010-06-23 13 views
5

Sto provando a creare più schede, ciascuna con un'attività diversa. L'unico svantaggio è che sto usando un file di layout personalizzato, quindi la mia classe estende un'attività piuttosto che uno TabActivity. Durante il tentativo di esecuzione, fallisce e suggerisce di chiamare TabHost.Setup(ActivityGroupManager agm)Android TabHost - Attività all'interno di ciascuna scheda

Qualcuno ha un'idea/un esempio pratico di come questo può essere ottenuto?

Grazie in anticipo

+0

"... ognuno con una diversa attività" - probabilmente vuol dire diverso "Visualizza". –

+0

L'estensione di 'TabActivity' è necessaria solo per l'attività principale che ospita le schede. Tutte le altre attività nelle schede possono semplicemente estendere l'attività e verranno eseguite correttamente. – codinguser

risposta

6

Questo è un esempio della mia attività che anche non si estende da TabActivity:

protected TabHost tabs; 

// ... 

/** 
* Init tabs. 
*/ 
private void initTabs() { 
    tabs = (TabHost) findViewById(R.id.tabhost); 
    tabs.setup(); 
    tabs.setBackgroundResource(R.drawable.bg_midgray); 

    TabHost.TabSpec spec; 

    // Location info 
    txtTabInfo = new TextView(this); 
    txtTabInfo.setText("INFO"); 
    txtTabInfo.setPadding(0, 0, 0, 0); 
    txtTabInfo.setTextSize(14); 
    txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_inactive_right_inactive); 
    txtTabInfo.setTextColor(Color.DKGRAY); 
    txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP); 
    txtTabInfo.setHeight(39); 
    spec = tabs.newTabSpec("tabInfo"); 
    spec.setContent(R.id.tabInfo); 
    spec.setIndicator(txtTabInfo); 
    tabs.addTab(spec); 

    // Maps 
    txtTabMap = new TextView(this); 
    txtTabMap.setText("MAP"); 
    txtTabMap.setTextSize(14); 
    txtTabMap.setPadding(0, 0, 0, 0); 
    txtTabMap.setBackgroundResource(R.drawable.bg_tab_middle_inactive_right_active); 
    txtTabMap.setTextColor(Color.DKGRAY); 
    txtTabMap.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP); 
    txtTabMap.setHeight(39); 
    spec = tabs.newTabSpec("tabMap"); 
    spec.setContent(R.id.tabMap); 
    spec.setIndicator(txtTabMap); 
    tabs.addTab(spec); 

    tabs.setCurrentTab(0); 

    tabs.setOnTabChangedListener(this); 
} 

// ... 
+0

Thnaks, ora funziona come un incantesimo. – Muniu

+0

grande ha funzionato. per favore accetta la risposta cliccando sul segno di spunta, se è stato utile :) –

1

fare una classe supplementare che si estende TabActivity e fare quella classe l'attività principale.

di farlo in tuo XML manifesti si includono:

<activity android:name=".TabActivtyClass" android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

In questa classe si potrebbe scrivere qualcosa di simile:

public class TabActivtyClass extends TabActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     TabHost tabHost = getTabHost(); // The associated TabHost 

     // Create an Intent to launch given Activty for this tab 
     Intent i = new Intent().setClass(this, FirstActivty.class); 
     TabHost.TabSpec spec = tabHost.newTabSpec("tab_name").setIndicator("Tab Name").setContent(i); // <- references the intent we just created 
     tabHost.addTab(spec); 

     // And do the same for the other tabs ... 
    } 
} 


Questa classe TabActivty può essere grande o piccolo come ti piacerebbe, ma in genere sarebbe lo schermo intero, con l'attività di ogni scheda caricata nella parte principale dello schermo, in questo modo: Example http://developer.android.com/resources/tutorials/views/images/hello-tabwidget.png


P.S. Inoltre, tenere presente che Eclipse Layout Editor non funziona con le schede. È a bug which has already been logged.

2

In primo luogo, definire un frametab nel layout principale.

<tabhost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> 
    <tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> 
     </framelayout> 
    </tabwidget> 
</linearlayout> 
</tabhost> 

Quindi, creare un'attività si estende da TabActivity

Resources res = getResources(); 
TabHost tabHost = getTabHost(); 
TabHost.TabSpec spec; 
Intent intent; 
intent = new Intent().setClass(this, DashboardActivity.class); 
spec = tabHost.newTabSpec("home").setIndicator("Home", res.getDrawable (R.drawable.ic_tab_dashboard)).setContent(intent); 
tabHost.addTab(spec); 
intent = new Intent().setClass(this, CreditCardActivity.class); 
spec = tabHost.newTabSpec("sample1").setIndicator("Sample Tab",res.getDrawable (R.drawable.ic_tab_sample1)).setContent(intent); 
tabHost.addTab(spec); 

Se si vuole scheda rolover, l'uso di layout selettore:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/helpblue" android:state_selected="true"> 
    <item android:drawable="@drawable/helpgray"></item> 
</item></selector> 

Ecco le immagini di esempio.

alt text http://rayyildiz.com/wp-content/uploads/2010/06/android_sample_tab-201x300.pngalt text http://rayyildiz.com/wp-content/uploads/2010/06/android_sample_tab2-201x300.png

Problemi correlati