0

我已經安裝了我的tabhost背景顏色,但它不工作。Android - tabhost不改變背景顏色

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item> 
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item> 
</style> 

<style name="ActionBarTabStyle" 
    parent="@style/Widget.AppCompat.ActionBar.TabView"> 
    <item name="android:background">@drawable/tab_bg_selector</item> 
    <item name="background">@drawable/tab_bg_selector</item> 
</style> 

tab_bg_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/blueLight" android:state_pressed="true"/> 
    <item android:drawable="@color/blueLight" android:state_selected="true"/> 
    <item android:drawable="@color/blueDark"/> 
</selector> 

bottom_tabs.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 
    </android.support.v4.app.FragmentTabHost> 

</LinearLayout> 

懇求建議。謝謝。

+0

您使用AppCompat庫嗎? – shkschneider

+0

是的,編譯'com.android.support:appcompat-v7:22.1.0' –

+0

你可以發佈tabvew –

回答

0

如果你確定而改變顏色的代碼比你可以試試這個:

tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
    public void onTabChanged(String arg0) { 
     for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) { 
      tab.getTabWidget().getChildAt(i) 
       .setBackgroundResource(R.drawable.tab_selected); // unselected 
     } 

     tab.getTabWidget().getChildAt(tab.getCurrentTab()) 
      .setBackgroundResource(R.drawable.tab_unselected); // selected 
    } 
}); 
+0

我無法實現OnTabChangeListener,因爲我使用的是ActionBarActivity,而不是TabActivity –

0

由於您使用程序兼容性庫,使用此:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item> 
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item> 
</style> 

<style name="ActionBarTabStyle" 
     parent="@style/Widget.AppCompat.ActionBar.TabView"> 
    <item name="android:background">@drawable/tab_bg_selector</item> 
    <item name="background">@drawable/tab_bg_selector</item> 
</style> 

您所需要的Widget.AppCompat.ActionBar.TabView家長和用於AppCompat支持的android:變體。

這是根據ActionBar's documentation和​​。

+0

這不起作用,請檢查我編輯的問題。 –

+0

是的,你也有'ActionBarTabStyle'錯誤。它應該有'Widget.AppCompat.ActionBar.TabView'作爲父項,而不是'android:Widget.ActionBar.TabView'。再次檢查:) – shkschneider

+0

仍然無法正常工作,我已經更新了該問題。 –

0

,您可以嘗試:

tab_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- Non focused states --> 
    <item android:drawable="@drawable/unselected_tab" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/> 
    <item android:drawable="@drawable/button_buynow" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/> 
    <!-- Focused states --> 
    <item android:drawable="@drawable/unselected_tab" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/> 
    <item android:drawable="@drawable/selected_tab" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/> 
    <!-- Pressed --> 
    <!-- Non focused states --> 
    <item android:drawable="@drawable/unselected_tab" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/> 
    <item android:drawable="@drawable/button_buynow" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/> 
    <!-- Focused states --> 
    <item android:drawable="@drawable/unselected_tab" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/> 
    <item android:drawable="@drawable/button_buynow" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/> 

</selector> 

MainActivity聲明在你之後,你可以用下面的代碼:

TabWidget widget = th.getTabWidget(); 

    for (int i = 0; i < widget.getChildCount(); i++) { 
     View v = widget.getChildAt(i); 
     v.setBackgroundResource(R.drawable.tab_selector); 
    } 
+0

我想要的是改變背景顏色,而不是背景圖像。 –

+0

TextView tv =(TextView)th.getTabWidget()。getChildAt(1) \t \t \t \t .findViewById(android.R.id.title); // Unselected Tabs \t \t \t \t tv.setBackgroundColor(getResources()。getColor(R.color.TransparentColor)); – BSavaliya

+0

對不起,我真的不明白。 –

0

在你的主題,加入以下內容

<item name="android:actionBarStyle">@style/MyActionBar</item> 
<item name="actionBarStyle">@style/MyActionBar</item> 

然後加入MyActionBar風格(換成你自己繪製項):

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
    <!-- action bar background --> 
    <item name="background">@drawable/ab_solid_mytabhost</item> 
    <!-- needed for 'stacked' & 'split' action bar (used by tabhost) --> 
    <item name="backgroundStacked">@drawable/ab_stacked_solid_mytabhost</item> 
    <item name="backgroundSplit">@drawable/ab_bottom_solid_mytabhost</item> 
</style> 

你styles.xml應該是這個樣子:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item> 
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item> 
</style> 

<style name="ActionBarTabStyle" parent="@style/Widget.AppCompat.ActionBar.TabView"> 
    <item name="android:background">@drawable/tab_bg_selector</item> 
    <item name="background">@drawable/tab_bg_selector</item> 
    <!-- add these --> 
    <item name="android:actionBarStyle">@style/MyActionBar</item> 
    <item name="actionBarStyle">@style/MyActionBar</item> 
</style> 

<!-- add this--> 
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
    <!-- action bar background --> 
    <item name="background">@drawable/ab_solid_mytabhost</item> 
    <!-- needed for 'stacked' & 'split' action bar (used by tabhost) --> 
    <item name="backgroundStacked">@drawable/ab_stacked_solid_mytabhost</item> 
    <item name="backgroundSplit">@drawable/ab_bottom_solid_mytabhost</item> 
</style> 

對於工作示例,這裏是一個鏈接到一個示例項目:https://github.com/ebernie/MyTabHostThemeSample