2015-07-13 72 views
-1

任何人都可以建议我如何设置选项卡小部件的背景颜色。我尝试了一些代码和网络中的例子,但仍然无法改变颜色。提前感谢家伙。 下面是我的代码:如何在android中加载时更改默认的tabwidget颜色?

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("My Families", getResources().getDrawable(R.drawable.tabselect)), 
         Main.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("My Pictures", getResources().getDrawable(R.drawable.tabunselect)), 
        Second.class, null); 

低于我的xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#2B78E4" /> 
</shape> 
<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#999999" /> 
</shape> 
+0

您可以检查以下的答案http://stackoverflow.com/questions/20404907/how-to-set-background-color-tabhost – Nishant

+0

很好第一次加载时背景颜色不会改变,只有点击它改变的标签。我第一次加载的时候需要的是什么,这意味着oncreate()被调用,我必须显示不同颜色的选项卡,而不是黑色和merun。 – Vicky

回答

2

你可以写在 for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) tab.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_selected); tab.getTabWidget().getChildAt(tab.getCurrentTab()).setBackgroundResource(R.drawable.tab_unselected);

将下面的代码onCreate()方法

相关问题