Domanda

Questa è la mia prima applicazione a schede per Android. Ho attraversato l'app "HellotabWidget" sul sito Web Androids, ma non riesco a capire come aggiungere contenuti alle schede. Il framelayout impila roba uno sopra l'altro in alto a sinistra (da quello che ho letto). Ho aggiunto un paio di TextViews e una VisualView, ma visualizza solo l'ultimo elemento aggiunto. C'è un modo per utilizzare il layout lineare anziché il layout della cornice? In caso contrario, come puoi posizionare più viste nella scheda? L'unica cosa che ho fatto diverso dall'esempio è che ho aggiunto una 4a scheda. In una delle attività della scheda ho inserito il seguente codice per provare a ottenere più elementi da visualizzare:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        TextView textview = new TextView(this);
        textview.setText("This is the About tab");
        setContentView(textview);

        TextView textview2 = new TextView(this);
        textview2.setText("About Test");
        setContentView(textview2);

        ImageView imgView = new ImageView(this);
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.header));
        setContentView(imgView);
    }

Ecco il link all'esempio che ho seguito:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

È stato utile?

Soluzione

Qual è il tuo file XML di layout? Lo uso e posso impilare diversi TextView in ogni scheda. Tabattività:

<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">
    <ScrollView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" >
        <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" />
            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" />
        </LinearLayout>
    </ScrollView>
</TabHost>

Attività all'interno della scheda:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView android:id="@+id/textOne" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/textTwo" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
<LinearLayout>

Anch'io sono un principiante, quindi potrebbe esserci un modo migliore per farlo. Questo funziona per me, però.

Altri suggerimenti

Non hai letto attentamente l'esempio. Dai un'occhiata al punto numero 6; vedrai qualcosa come:

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

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ArtistsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AlbumsActivity.class);
    spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SongsActivity.class);
    spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                      res.getDrawable(R.drawable.ic_tab_songs))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(2);
}

Questo è quello che hai messo nell'oncrea per il TabActivity. Come puoi vedere ha 3 attività. Quello che stai facendo è usare solo un'attività e l'impostazione della vista del contenuto 3 volte, il che è ovviamente sbagliato.

Allora ... come farlo funzionare? Innanzitutto, leggi di nuovo il tutorial. In secondo luogo, crea un'attività per ogni scheda che desideri mostrare. E usa il modello sopra per aggiungere tali attività al tuo TabHost.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top