如何设置要在操作栏中显示的选项卡数量?

时间:2014-07-03 21:35:43

标签: android

如何控制操作栏中显示的标签数量?
要设置标签,请使用以下代码:

public class MainActivity extends ActionBarActivity implements TabListener {

    private List<Fragment> fragList = new ArrayList<Fragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        Tab tabVipusk = bar.newTab();
        tabVipusk.setText("Выпуск");
        tabVipusk.setTabListener(this);
        bar.addTab(tabVipusk);
        Tab tabConstuctor = bar.newTab();
        tabConstuctor.setText("Конструктор");
        tabConstuctor.setTabListener(this);
        bar.addTab(tabConstuctor);
        Tab tabPopular = bar.newTab();
        tabPopular.setText("Популярное");
        tabPopular.setTabListener(this);
        bar.addTab(tabPopular);
        Tab tabActual = bar.newTab();
        tabActual.setText("Актуальное");
        tabActual.setTabListener(this);

        bar.addTab(tabActual);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        if (fragList.size() > 0 && fragList.size() >= tab.getPosition()) {
            ft.replace(android.R.id.content, fragList.get(tab.getPosition()));
        } else {
            fragList.add(new VipuskTabFragment());
            fragList.add(new ConstructorTabFragment());
            fragList.add(new PopularTabFragment());
            fragList.add(new ActualTabFragment());
            ft.replace(android.R.id.content, fragList.get(tab.getPosition()));
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // if (fragList.size() >= tab.getPosition()) {
        // ft.remove(fragList.get(tab.getPosition()));
        // }

    }

}

但在纵向模式下,它只显示3个标签。但没有边距,第四个标签将明显适合。所以实际上有4个标签的地方。如何显示4个标签?

1 个答案:

答案 0 :(得分:0)

我使用了FragmentTabHost,它使用固定标签,非常适合我的事业