【发布时间】:2025-05-05 04:25:01
【问题描述】:
我有一个带有扩展 TabActivity 的 MainActivity 的应用程序(我知道它已被弃用,但要更改整个应用程序要做的事情太多了)。
所以在我的应用程序中,我使用 tabhost 创建 3 个这样的标签:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
firstTabSpec.setIndicator("tab1").setContent(
new Intent(this, tab1.class));
secondTabSpec.setIndicator("tab2").setContent(
new Intent(this, tab2.class));
thirdTabSpec.setIndicator("tab3").setContent(
new Intent(this, tab3.class));
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
//Changing the tabs text color on the tabs
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#ffffff"));
}
// remove divider
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
所以我的代码创建了链接到 3 个不同活动的 3 个选项卡并设置了选项卡的颜色。第一个选项卡首先加载的颜色与其他两个不同。
我希望选项卡的颜色根据选择的选项卡而改变。 因此,当我按下第二个选项卡时,我希望第一个获得#607d8b 颜色,第二个获得#90a4ae。 第三个也一样。
尝试实现 OnTabChangeListener 但无法使其工作。 尝试这样做:
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
在每个加载的选项卡活动中更改了颜色,但我收到错误,它无法解析 tabhost(因为它应该在 MainActivity 中定义。
【问题讨论】:
标签: android tabs android-tabhost background-color