我设计了一个应用程序,因为我正在使用TabBar
,我想在选择或按下时更改TabBar
的BackgroundColor ..在我正在实现的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));
}
}
我还尝试将Custom_tab.xml
实现为
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/blue"/>
<item android:state_focused="true" android:drawable="@drawable/new_background"/>
<item android:state_pressed="true" android:drawable="@drawable/blue"/>
</selector>
并在
中调用此文件<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"/>
仍然没有工作..我错过了什么?
答案 0 :(得分:1)
在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));
和TabChanged
方法
@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));
}
它将显示所选标签的蓝色和名为new_background的白色图像到未选中的标签。
并且不要忘记写
implements OnTabChangeListener
在您的班级名称
之后和
tabHost.setOnTabChangedListener(this);
后
tabHost = (TabHost) v.findViewById(R.id.tabhost);
我想这是完美解决方案。