答案 0 :(得分:0)
要使用ActionBar创建选项卡,您需要启用NAVIGATION_MODE_TABS,然后创建ActionBar.Tab的多个实例,并为每个实例提供ActionBar.TabListener接口的实现。例如,在活动的onCreate()方法中,您可以使用类似于以下代码:
@Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(actionBar.newTab().setText("Tab " + (i + 1)).setTabListener(tabListener));
}
}
如何处理ActionBar.TabListener回调以更改选项卡取决于您构造内容的方式。但是,如果您如上所述使用ViewPager为每个选项卡使用片段,则以下部分将说明如何在用户选择一个选项卡时在页面之间切换以及如何在用户在页面之间滑动时更新所选的选项卡。
有关完整指南Check this out。
希望这会有所帮助。