【发布时间】:2013-08-20 18:57:02
【问题描述】:
我正在为 Android 4.3 进行开发,但我的代码出现了我似乎无法弄清楚的问题。我一直在寻找答案,但我能找到的只是那些想要我目前的情况而我想要他们的错误程序的人。
我在 Android 的 ActionBar 中的选项卡教程中将 3 个选项卡放在了操作栏中。
会发生什么: 标签应该出现在操作栏上
会发生什么: 选项卡显示在操作栏下方
我的问题是:
1。如何将这些选项卡设置为显示在 ActionBar 上而不是下方
2。如果成功 1,我该如何设置标签的大小?例如使 3 个选项卡一起占据 ActionBar 的三分之一(按宽度)
我的代码:
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
final ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
mPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
mPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
actionBar.setSelectedNavigationItem(position);
}
});
// Add 3 tabs, specifying the tab's text and TabListener
actionBar.addTab(
actionBar.newTab()
.setText("A")
.setTabListener(tabListener));
actionBar.addTab(
actionBar.newTab()
.setText("B")
.setTabListener(tabListener));
actionBar.addTab(
actionBar.newTab()
.setText("C")
.setTabListener(tabListener));
【问题讨论】:
-
您是在平板电脑还是手机上进行测试?在较小的屏幕上,ActionBar 选项卡显示在 ActionBar 下方。在此处查看图 7 和 8:developer.android.com/guide/topics/ui/actionbar.html#Tabs
-
我正在手机上进行测试,当我旋转我的设备时,选项卡会显示在 AcitonBar 中。我希望这种情况永远发生。
标签: android android-actionbar android-tabs