4

如何在单击选项卡主机上更改颜色。我可以改变图像,但颜色不会改变。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);`enter code here`
        spec1.setIndicator("Tab 1", getResources().getDrawable(setBackgroundColor(Color.RED));

        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2", getResources().getDrawable(setBackgroundColor(Color.GREAN));
        spec2.setContent(R.id.tab2);

        TabSpec spec3=tabHost.newTabSpec("Tab 3");
        spec3.setIndicator("Tab 3", getResources().getDrawable(setBackgroundColor(Color.BLACK));
        spec3.setContent(R.id.tab3);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
    }
}
4

4 回答 4

4

您可以使用此功能,只需在其中传递您的 tabHost

public static void setTabColor(TabHost tabhost) {
        for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
        {
            tabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.white); //unselected
        }
        tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundResource(R.color.tab_selected); // selected
    }
于 2012-07-07T10:14:11.427 回答
1

//设置标签的背景颜色

   for (int i = 1; i <= 3; i++) {
    tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundColor(getResources().getColor(R.color.gradient));

       }
于 2012-07-07T10:11:34.827 回答
1
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);`enter code here`
        spec1.setIndicator("Tab 1", getResources().getDrawable(setBackgroundColor(view.Color.RED));

        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2", getResources().getDrawable(setBackgroundColor(view.Color.GREAN));
        spec2.setContent(R.id.tab2);

        TabSpec spec3=tabHost.newTabSpec("Tab 3");
        spec3.setIndicator("Tab 3", getResources().getDrawable(setBackgroundColor(view.Color.BLACK));
        spec3.setContent(R.id.tab3);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
    }
}
于 2013-05-22T10:23:59.047 回答
-3

为此,您应该使用事件 onTabChangedlistener 并在其中放置您想要的任何循环,如所选答案中所示。

tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String s) {
            setTabColor(tabhost); 
        }
    });

你可以阅读这篇文章,这会对你有所帮助,但它是西班牙语的。

http://www.johnarij.ml/2017/04/19/cambio-de-color-de-tab-en-tabhost-android/

于 2017-04-19T14:50:38.983 回答