这是我的代码,选择选项卡时图标会更改,未选中时会更改回来。
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setBackgroundDrawable(null);
icons = new int[] {
R.drawable.ic_facebook,
R.drawable.ic_facebook,
R.drawable.ic_facebook,
R.drawable.ic_facebook,
};
icons_selected = new int[] {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setIcon(icons[i])
.setTabListener(this)
);
}
}
和这个
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction){
getActionBar().getSelectedTab().setIcon(icons_selected[tab.getPosition()]);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
getActionBar().getSelectedTab().setIcon(icons[tab.getPosition()]);
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}