J'utilise des onglets dans mon application Android, et je l'ai rencontré ce problème lors de l'exécution de l'application sur les téléphones HTC Sense:style personnalisé pour TabWidget Android
Android: Highlighted tab of TabWidget not readable on HTC Sense
La solution, il a proposé (réglage android: targetSdkVersion à 4) ne résout pas le problème pour moi, et je ne veux pas mettre le sdk cible à 4.
J'ai plutôt essayé de résoudre ce problème en créant mon propre style de widget tabulation, et en modifiant la couleur du texte. Le problème est qu'il n'y a pas de différence notable quand j'utilise mon propre style; c'est-à-dire que le style ne semble pas être appliqué aux onglets.
C'est le code pour l'activité principale, tenant les onglets:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1").setContent(new Intent(this, Tab1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2").setContent(new Intent(this, Tab2.class)));
tabHost.setCurrentTab(0);
}
Ceci est mon tab.xml. Notez que j'ai spécifié MyTabStyle comme style pour TabWidget:
<?xml version="1.0" encoding="utf-8"?>
<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
style="@style/MyTabStyle"
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" />
</LinearLayout>
</TabHost>
Et voici ma définition de MyTabStyle, que je l'ai défini dans res/valeurs/styles.xml:
<style name="MyTabStyle" parent="@android:style/TextAppearance.Widget.TabWidget">
<item name="android:textColor">#5DFC0A</item>
</style>
Pourquoi aucun changement dans MyTabStyle apparaître dans mon application? D'autres solutions pour résoudre le texte invisible sur les onglets sélectionnés dans HTC Sense?
MISE À JOUR 2011-06-02
j'ai réussi à résoudre ce dans une sorte de façon aki, en utilisant les connaissances que le texte sur les onglets sont en fait TextViews. Ajoutez la méthode suivante à votre activité:
private void setTabColor(TabHost tabHost) {
try {
for (int i=0; i < tabHost.getTabWidget().getChildCount();i++) {
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
if (tv != null) {
tv.setTextColor(Color.parseColor("#ffffff"));
}
TextView tv2 = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); // Selected Tab
if (tv2 != null) {
tv2.setTextColor(Color.parseColor("#000000"));
}
}
} catch (ClassCastException e) {
// A precaution, in case Google changes from a TextView on the tabs.
}
}
Ajouter ce qui suit dans votre méthode onCreate() dans votre activité:
// Only apply the fix if this is a HTC device.
if (android.os.Build.MANUFACTURER.toLowerCase().contains("htc")) {
// Set up the color initially
setTabColor(tabHost);
// Add a tab change listener, which calls a method that sets the text color.
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
setTabColor(tabHost);
}
});
}
Relié, mais pas exactement un doublon, mais a une meilleure réponse (plus récente?) Pour comment styliser le TabWidget: http://stackoverflow.com/a/2805256/2291 –