如何在开始时更改tabHost?

时间:2016-08-31 06:46:12

标签: java android android-studio android-tabhost android-drawable

当我启动活动时,3个选项卡是旧选项卡样式,而不是setBackgroundResource(R.drawable.button);.

但只有标签的新样式才会出现,选择其中一个标签后。 下面是我的代码,让我知道如何更改我的代码,以便3个选项卡可以显示新的样式?

代码:

final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabHost.TabSpec spec;
Intent intent;

spec = tabHost.newTabSpec("1");
spec.setIndicator("Inventar 1");
intent = new Intent(this, Spielregeln.class);
spec.setContent(intent);
tabHost.addTab(spec.setIndicator(" ", ResourcesCompat.getDrawable(getResources(), R.drawable.handwerk, null)));

spec = tabHost.newTabSpec("2");
spec.setIndicator("Inventar 2");
intent = new Intent(this, Spielregeln.class);
spec.setContent(intent);
tabHost.addTab(spec.setIndicator(" ", ResourcesCompat.getDrawable(getResources(), R.drawable.freunde, null)));

spec = tabHost.newTabSpec("3");
spec.setIndicator("Inventar 3");
intent = new Intent(this, Spielregeln.class);
spec.setContent(intent);
tabHost.addTab(spec.setIndicator(" ", ResourcesCompat.getDrawable(getResources(), R.drawable.inventar, null)));

tabHost.setCurrentTab(0);

tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String arg0) {
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.button); // unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                .setBackgroundResource(R.drawable.button_clicked); // selected

    }
});

0 个答案:

没有答案