Android avec onglet Mise en page setContent
Je suis le développement d'une application Android avec des onglets de mise en page. J'ai réussi à le où il n'est pas pondre une nouvelle activité comme Google tutoriel proposé, cependant je ne l'a fait dans un effort pour obtenir mon contenu à afficher lorsque vous cliquez sur chaque onglet. Actuellement, il montre juste noir, indépendamment de ce que l'onglet actif.
Voici mon code, dans sa dernière itération:
Main.java
public class Main extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
//add orders tab
spec = tabHost.newTabSpec("orders").setIndicator("Orders",
res.getDrawable(R.drawable.flash_36))
.setContent(R.id.ordersLayout);
tabHost.addTab(spec);
//add positions tab
spec = tabHost.newTabSpec("positions").setIndicator("Positions",
res.getDrawable(R.drawable.small_tiles_36))
.setContent(R.id.positionsLayout);
tabHost.addTab(spec);
//add strategies tab
spec = tabHost.newTabSpec("strategies").setIndicator("Strategies",
res.getDrawable(R.drawable.cards_36))
.setContent(R.id.strategiesLayout);
tabHost.addTab(spec);
//add account tab
spec = tabHost.newTabSpec("account").setIndicator("Account",
res.getDrawable(R.drawable.seal_36))
.setContent(R.id.accountLayout);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/mainLayout"
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">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:padding="5dp">
<include layout="@layout/orders"/>
<include layout="@layout/positions"/>
<include layout="@layout/strategies"/>
<include layout="@layout/account"/>
</FrameLayout>
</LinearLayout>
</TabHost>
Account.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/accountLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Account Information"
android:id="@+id/accountLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
Orders.xml
Je suis le seul à y compris les deux, ils sont tous exactement la même avec appropriée id pour les LinearLayout android:id de paramètre.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/ordersLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Working Orders"
android:id="@+id/ordersLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
Et voici une capture d'écran de ce que je reçois lors de la commutation entre les onglets:
De L'Onglet Commandes
Onglet Positions
Cela est vrai tant pour l'émulateur et un Samsung Galaxy je viens de recevoir, et j'en suis très recommandons par le chemin, des idées?
OriginalL'auteur zkwentz | 2010-09-16
Vous devez vous connecter pour publier un commentaire.
J'ai compris, et c'est un simple paramètre de plus à la main.xml fichier:
Main.xml (révisée)
Avis de la LinearLayout, il comprend maintenant le paramètre android:orientation. Ce qu'il faisait avant était de pousser tout le reste hors de l'écran vers la droite. Maintenant, il fonctionne comme il se doit.
Remarque: Au bureau demain, je peux tester si ce encore me permet de faire tourner le téléphone, et d'afficher correctement dans l'orientation paysage. Si non, alors je vais probablement créer un fichier xml distinct pour l'horizontale, sauf si quelqu'un a des idées dans ce cours.
OriginalL'auteur zkwentz