Tabs below action bar

删除回忆录丶 提交于 2019-11-29 23:50:17

问题


I'm developing for Android 4.3 and been having a problem with my code that I can't seem to figure out. I've been looking for answers for a while and all I could find was people that want my current situation while I want their bugged program.

I have 3 tabs which I've placed in the action bar by Android's tutorial for tabs in ActionBar.

What is supposed to happen: The tabs should appear on the action bar

What happens instead: The tabs appear below the action bar

My questions are:
1. How can I set those tabs to show on the ActionBar and not below
2. If succeeding 1, how can I set the size of the tabs? for example making the 3 tabs together take one third of the ActionBar (by width)

My code:

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));

回答1:


private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 3;

in 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;

    }
}

and your 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>

ADD:

You should replace this line:

actionBar.setDisplayHomeAsUpEnabled(true);

to this:

actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);



回答2:


Edit: For tabs, use this part of the API guides.

For adding elements to your ActionBar, you must Override onCreateOptionsMenu()

For example:

@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);
}

And you should have an xml for the layout of those items:

res/menu/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>

Source



来源:https://stackoverflow.com/questions/18342966/tabs-below-action-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!