2010-11-08 45 views
14

我使用的標籤在我的Android應用程序,我已經運行在HTC Sense的手機應用程序時,遇到這個問題自定義樣式: targetSdkVersion爲4)並不能解決這個問題,我也不想將目標sdk設置爲4.對Android的TabWidget

我反而嘗試解決這個問題,我創建了自己的選項卡小部件樣式,並修改了文本顏色。問題是,當我使用我自己的風格時,沒有什麼明顯的區別。即樣式似乎不適用於標籤。

這是主要活動代碼,拿着標籤:

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); 
} 

這是我的tab.xml。我已指定MyTabStyle的風格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> 

這是我MyTabStyle的定義,這是我在res /價值/ styles.xml定義:

<style name="MyTabStyle" parent="@android:style/TextAppearance.Widget.TabWidget"> 
    <item name="android:textColor">#5DFC0A</item> 
</style> 

爲什麼沒有變化在MyTabStyle中顯示在我的應用程序?任何其他解決方案來解決HTC Sense中選定選項卡上的不可見文本?

UPDATE 2011-06-02

我設法解決這個問題的排序哈克的方式,通過知識選項卡上的文字實際上是TextViews。下面的方法添加到您的活動:

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. 
    } 
} 

添加以下在onCreate()方法在你的活動:

// 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); 
     } 
    }); 
} 
+0

相關,但不完全重複,但有更好的(更新?)答案如何風格的TabWidget:http://stackoverflow.com/a/2805256/2291 – 2015-12-22 17:56:40

回答

-6

<!-- Focused states --> 
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 

<!-- Pressed --> 
<item android:state_pressed="true" android:drawable="@drawable/tab_press" /> 

+9

這是如何回答我的問題? – gizero 2011-02-16 21:44:06

+1

@gizero這是怎麼接受的答案呢? :) – hendrix 2013-02-01 13:58:06

+0

因爲這是你如何設計一個選項卡。 – Ajibola 2013-02-24 17:07:46

11

這傢伙已經發布,可以自定義有關選項卡都漂亮多了。適用於Android 1.6及以上版本:Custom Android Tabs

我還沒有嘗試過,但到達那裏。基本上你可以在你的TabSpec上調用setIndicator,並將它傳遞給一個視圖,而不僅僅是文本。然後你就可以。如果你還在使用上> 14 TabWidgets使用選擇等

+0

但遺憾的是你仍然需要至少分鐘sdk4。 – Fraggle 2011-03-10 18:03:34

+34

「這個傢伙」就是我。 – 2013-04-19 02:03:49

2

自定義視圖,看到http://code.google.com/p/android/issues/detail?id=2267。最後評論指向iosched2011也的確有他們更換了TabIndicator整個視圖。除了一些屬性(一般背景,文本)外,任何簡單的方式似乎都被破壞了,並且這種複雜性是必需的。