我正在为Android 4.3开发并且我的代码出现问题,我似乎无法弄明白。我一直在寻找答案,而我所能找到的只是那些想要我现在的情况而我想要他们的错误程序的人。
我有3个标签,我已经通过Android的ActionBar选项卡教程放置在操作栏中。
应该发生什么: 选项卡应显示在操作栏
上会发生什么: 选项卡显示在操作栏
下方 我的问题是:
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));
答案 0 :(得分:1)
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 3;
onCreate()中的
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabReselected(Tab tab,
android.app.FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab,
android.app.FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab,
android.app.FragmentTransaction ft) {
}
};
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar
.newTab()
.setText(mPagerAdapter.getPageTitle(i))
.setIcon(
((ScreenSlidePagerAdapter) mPagerAdapter)
.getPageIcon(i))
.setTabListener(tabListener));
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment.instantiate(MainActivity.this,
Fragment_sports.class.getName());
case 1:
return Fragment.instantiate(MainActivity.this,
Fragment_casino.class.getName());
case 2:
return Fragment.instantiate(MainActivity.this,
Fragment_live_betting.class.getName());
default:
break;
}
return null;
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public CharSequence getPageTitle(int position) {
String tabLabel = null;
switch (position) {
case 0:
tabLabel = " Sports";
break;
case 1:
tabLabel = "Casino";
break;
case 2:
tabLabel = "Live Betting";
break;
}
return tabLabel;
}
public int getPageIcon(int position) {
int id = 0;
switch (position) {
case 0:
id = R.drawable.icon_all_sports_d;
break;
case 1:
id = R.drawable.icon_favourites_d;
break;
case 2:
id = R.drawable.icon_live_d;
break;
default:
break;
}
return id;
}
}
和您的 main_activity.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
添加强>
你应该替换这一行:
actionBar.setDisplayHomeAsUpEnabled(true);
到此:
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
答案 1 :(得分:0)
修改:对于标签,请使用this部分API指南。
要向ActionBar
添加元素,您必须覆盖onCreateOptionsMenu()
例如:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
你应该有一个xml用于这些项目的布局:
RES /菜单/ main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"/>
<item android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:title="@string/action_compose" />
</menu>