octobre
2012

Bonjour,
Petite astuce en direct de la formation Android Initiation avancée qui se passe à Paris le 01-02-03 Octobre.
J’ai un stagiaire Romain Zanon, qui se demande comment faire en sorte d’avoir un TabHost avec la barre d’onglets qui défile. Il y a deux choix, soit on se trouve un librairie qui le fait (au risque d’être lié à un obscur développeur qui ne mettra jamais sa lib à jour et qui potentiellement a du code tout pourri), soit on réfléchi quelques secondes.
Tout d’abord, rappelons le code pour afficher des onglets:
Le code Xml du layout est le suivant:
<LinearLayout xmlns:android= »http://schemas.android.com/apk/res/android »
android:layout_width= »fill_parent »
android:layout_height= »fill_parent »
android:orientation= »vertical » >
<TabHost
android:id= »@+id/tabhost »
android:layout_width= »fill_parent »
android:layout_height= »fill_parent » >
<LinearLayout
xmlns:android= »http://schemas.android.com/apk/res/android »
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 » >
<DigitalClock
android:id= »@+id/digital »
android:layout_width= »fill_parent »
android:layout_height= »fill_parent »
android:gravity= »center » />
<AnalogClock
android:id= »@+id/analog »
android:layout_width= »fill_parent »
android:layout_height= »wrap_content »
android:layout_gravity= »center » />
<!–…d’autres onglets…–>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Bon, alors comment je fais pour avoir les onglets qui défilent ?o? je peux partir dans de grands hacks de code, tout ça, mais je peux aussi faire très simple, il me suffit de placer ma barre d’onglets dans un HorizontalScrollView et il défileront automatiquement.
Le code:
<LinearLayout xmlns:android= »http://schemas.android.com/apk/res/android »
android:layout_width= »fill_parent »
android:layout_height= »fill_parent »
android:orientation= »vertical » >
<TabHost
android:id= »@+id/tabhost »
android:layout_width= »fill_parent »
android:layout_height= »fill_parent » >
<LinearLayout
xmlns:android= »http://schemas.android.com/apk/res/android »
android:layout_width= »fill_parent »
android:layout_height= »fill_parent »
android:orientation= »vertical » >
<HorizontalScrollView
android:layout_width= »fill_parent »
android:layout_height= »wrap_content » >
<TabWidget
android:id= »@android:id/tabs »
android:layout_width= »wrap_content »
android:layout_height= »wrap_content » />
</HorizontalScrollView>
<FrameLayout
android:id= »@android:id/tabcontent »
android:layout_width= »fill_parent »
android:layout_height= »fill_parent » >
<DigitalClock
android:id= »@+id/digital »
android:layout_width= »wrap_content »
android:layout_height= »fill_parent »
android:background= »#AA00F0FF »
android:gravity= »center » />
<AnalogClock
android:id= »@+id/analog »
android:layout_width= »fill_parent »
android:layout_height= »wrap_content »
android:layout_gravity= »center »
android:background= »#AAFF00FF » />
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Et voilà, une barre d’onglets qui défilent, c’est compatible pour toutes les versions du système et vous vous n’avez qu’à modifier votre fichier de Layout pour le mettre en place. Autrement dit, dans 5 secondes votre TabHost possède une barre d’onglets défilante.
Prochaine formation Android2EE sur Paris du 05 au 09 Novembre 2012 ou du 27 au 30 Novembre sur Toulouse, vous aussi devenez un dieu en programmation Android.
Et puis merci qui ? merci Android2ee les gars de chez Google qui nous font ce superbe SDK !!! ![]()
ps:Le WordPress de DVP, c’est pas encore ça par contre…
Mathias Séguy
mathias.seguy@android2ee.com
Fondateur Android2EE
Formation – Expertise – Consulting Android.
Ebooks pour apprendre la programmation sous Android.

Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo
2 Commentaires + Ajouter un commentaire
Référence Android
Mots-clefs
Articles récents
- Comment forcer a rester dans une activité, le retour :)
- Mardi 19 Mars : Soirée Architecture Android avec Zenika et Android2EE par Mathias Séguy, rédacteur Android sur Developpez.com
- Formation Android de Ludequip par Android2EE de Janvier 2013 – Debriefing
- Comment forcer l’utilisateur à rester au sein de son activité
- Zoinz, l’application MusicPlayer de Romain Zanon, mon stagiaire d’octobre, est sur le store ! Yes !



Un article de MathiasSeguy


[...] JavaOne, JavaOne 2012 Onglet défilant (Tabhost avec un sliding du TabWidget), Formation Android. par MathiasSeguy (02/10/2012 12:16) Bonjour, Petite astuce en direct de la formation Android [...]
[...] d’avoir un TabHost avec la barre d’onglets qui défile .Il y a deux choix … http://ANDROID-ACTUALITES.COM actualite sur android vous propose la suite …. Publié dans application | Tags : accompagnees, programmation, recueillis, [...]