【发布时间】:2014-12-16 20:45:54
【问题描述】:
在 Android 5.0 之前制作如下滑动选项卡式布局:
只需要以下代码:
@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 上的导航方法已被弃用,我如何实现这种类型的视图以与 Android 5.0 及之前版本兼容?
【问题讨论】:
-
你已经问过这个问题并得到了答案
-
不,这是一个不同的问题。在stackoverflow.com/questions/27461578/… 中,我正在处理基本选项卡,而在这个问题中,我正在查看滑动视图。
-
@Sauron 你想要
ViewPager+Sliding Tabs,你使用的是旧的设计模式。
标签: android android-5.0-lollipop android-tabs