Android - 更改Tab onClicked的背景颜色

时间:2014-01-10 09:31:26

标签: android android-tabhost

我设计了一个应用程序,因为我正在使用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"/>

仍然没有工作..我错过了什么?

1 个答案:

答案 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);

我想这是完美解决方案