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
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 に答える
1

// タブの背景色を設定

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

       }
于 2012-07-07T10:11:34.827 に答える