Android-单击后更改选项卡的背景色
我设计了一个应用程序,我正在使用Android-单击后更改选项卡的背景色,android,android-tabhost,Android,Android Tabhost,我设计了一个应用程序,我正在使用TabBar,我想在选择或按下时更改TabBar的背景色。。在我实现的类中,TabBar正在扩展Fragment而不是TabActivity。我已经实现了以下功能 tabHost = (TabHost) v.findViewById(R.id.tabhost); mLocalActivityManager = new LocalActivityManager(getActivity(), false); Intent intentongo
TabBar
,我想在选择或按下时更改TabBar
的背景色。。在我实现的类中,TabBar
正在扩展Fragment
而不是TabActivity
。我已经实现了以下功能
tabHost = (TabHost) v.findViewById(R.id.tabhost);
mLocalActivityManager = new LocalActivityManager(getActivity(), false);
Intent intentongoing = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecOngoing = tabHost.newTabSpec("On Going").setIndicator("On Going").setContent(intentongoing);
Intent intentcomplete = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecComplete = tabHost.newTabSpec("Completed").setIndicator("Completed").setContent(intentcomplete);
Intent intentproposed = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecProposed = tabHost.newTabSpec("Proposed").setIndicator("Proposed").setContent(intentproposed);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
tabHost.addTab(tabSpecOngoing);
tabHost.addTab(tabSpecComplete);
tabHost.addTab(tabSpecProposed);
tabHost.setCurrentTab(0);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setPadding(2,5,0,10);
Log.v("","In for loop");
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.WHITE);
}
**OnCreate Ends**
@Override
public void onTabChanged(String arg0)
{
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
}
}
并调用此文件
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="40dp"
android:layout_gravity="center" android:gravity="center" android:background="@drawable/custom_tab"/>
仍然不工作。。我缺少什么???在
OnCreate中使用它
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setPadding(2,5,0,10);
Log.v("","In for loop");
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.WHITE);
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.new_background);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
它将向选中的选项卡显示蓝色,并向未选中的选项卡显示名为new_background的白色图像
别忘了写信
implements OnTabChangeListener
在你的班名之后
及
之后
这是一个完美的解决方案。我想。当你不使用I变量时,last for循环是无用的。。。。您在哪里将侦听器设置为TabHost?如果您在onTabChanged(…)
TabHost.getCurrentTabView().setBackgroundColor(…)Class属性扩展片段实现OnTabChangeListener(…)中使用它,会怎么样?TabHost.setOnTabChangedListener(此)代码>丢失
@Override
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.new_background);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
}
implements OnTabChangeListener
tabHost.setOnTabChangedListener(this);
tabHost = (TabHost) v.findViewById(R.id.tabhost);