这是我的MainActivity
public class MainActivity extends Activity {
// Declaring our tabs and the corresponding fragments.
ActionBar.Tab photosTab, videosTab;
Fragment photoTab = new Photos();
Fragment videoTab = new Videos();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Asking for the default ActionBar element that our platform supports.
ActionBar actionBar = getActionBar();
// Screen handling while hiding ActionBar icon.
actionBar.setDisplayShowHomeEnabled(false);
// Screen handling while hiding Actionbar title.
actionBar.setDisplayShowTitleEnabled(false);
// Creating ActionBar tabs.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Setting custom tab icons.
photosTab = actionBar.newTab().setText("PHOTOS");
videosTab = actionBar.newTab().setText("VIDEOS");
// Setting tab listeners.
photosTab.setTabListener(new TabListener(photoTab));
videosTab.setTabListener(new TabListener(videoTab));
// Adding tabs to the ActionBar.
actionBar.addTab(photosTab);
actionBar.addTab(videosTab);
}
}
这是TabListener类
public class TabListener implements ActionBar.TabListener {
private Fragment fragment;
// The contructor.
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
// When a tab is tapped, the FragmentTransaction replaces
// the content of our main layout with the specified fragment;
// that's why we declared an id for the main layout.
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.activity_main, fragment);
}
// When a tab is unselected, we have to hide it from the user's view.
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
// Nothing special here. Fragments already did the job.
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
在tabListener类中,我在onTabUnselected方法中使用了switch case,但它没有用......而且我在mainActivity中使用了setBackgroundDrawable ....没有用...