Question

J'utilise le exmaple de Google de Bonjour, TabWidget mais modifié pour ressembler à ceci:

main.xml:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <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">
            <TextView 
                android:text="@+layout/text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is another tab" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

fichier java:

public class HelloTabWidget extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost mTabHost = getTabHost();

        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.text));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));

        mTabHost.setCurrentTab(0);
    }
}

et voici le text.xml dans res / layout:

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="This is Tab 1" />
</LinearLayout>

Ce que je veux essentiellement essayer de faire est d'avoir chaque onglet se référer à son propre fichier xml plutôt que dans tous main.xml, mais le texte dans le premier onglet ne se présente pas.

Était-ce utile?

La solution

Vous essayez d'avoir chaque onglet se référer à son propre Activity? Si oui, vous pouvez configurer l'intention que le contenu de chaque onglet:

intent = new Intent().setClass(this, Test.class);       
spec = tabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(intent);
        tabHost.addTab(spec);

Bien sûr, vous auriez alors d'avoir un test de classe nommée (ou tout autre chose) qui définit test.xml comme la mise en page.

public class Test extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    }
}

Ce tutoriel vous aidera encore si tel est le type de scénario que vous essayez de mettre en place.

Autres conseils

Il y a trois options pour les onglets en utilisant l'hôte onglet intégré.

  1. vues multiples sur une seule activité avec un fichier de mise en page
  2. Activités multiples
  3. Les vues personnalisées construit dans un TabContentFactory

On dirait que vous voulez # 3. Ce que vous devez faire est quelque chose comme ceci:

setContent(new TabHost.TabContentFactory() {
    public View createTabContent(String tag)
    {
        View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);
        // Setup the view here
        return view;
    }});
View view = LayoutInflater.from(HelloTabWidget.this).inflate(R.layout.text);

Ce n'est pas tout à fait raison que LayoutInflater.inflate prend deux paramètres.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top