如何在Android中设置标签视图指示器背景颜色

时间:2012-07-07 08:15:48

标签: android

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

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 个答案:

答案 0 :(得分: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
    }

答案 1 :(得分:1)

//设置标签的背景颜色

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

       }

答案 2 :(得分: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);
    }
}

答案 3 :(得分:-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/