Tab Bar in Android

2020-08-01 07:01发布

问题:

With the help of these program, I created the Tabs. you can look at a below Codes.

HomeActivity.java:

 package com.sit.fth.activity;

    import android.app.ActionBar;
    import android.app.ActionBar.Tab;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.view.ViewPager;
    import android.util.Log;
    import android.view.WindowManager;


    public class HomeActivity extends BaseActivity implements
            VideoFragment.OnVideoSelectedListener,

            AboutFragment.OnAboutFragmentSelectedListener,

            AnnouncenentFragment.OnAnnouncementSelectedListener,

            GalleryFragment.OnGalItemSelectedListener,

            MagzineFragment.OnMagzineSelectedListener {

        private ActionBar actionabar;
        private ViewPager viewpager;
        private LiveStreamFragment liveStreamFragment;
        private AppData appData;
        private FragmentManager fm;
        private MyFragmentPagerAdapter fragmentPagerAdapter;
        // private HomeBaseFragment fragment;

        @Override
        protected void onCreate(Bundle arg0) {
            super.onCreate(arg0);

            setContentView(R.layout.activity_main);
            appData = ((GemsApplication) this.getApplication()).getAppData();


            actionabar = getActionBar();
            actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



            liveStreamFragment = new LiveStreamFragment();

            viewpager = (ViewPager) findViewById(R.id.pager);
            fm = getSupportFragmentManager();

            /** Defining a listener for pageChange */
            ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    super.onPageSelected(position);
                    actionabar.setSelectedNavigationItem(position);
                }
            };

            /** Setting the pageChange listner to the viewPager */
            viewpager.setOnPageChangeListener(pageChangeListener);

            /** Creating an instance of FragmentPagerAdapter */
            fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);

            viewpager.setAdapter(fragmentPagerAdapter);
            actionabar.setDisplayShowTitleEnabled(true);

            /** Defining tab listener */
            ActionBar.TabListener tabListener = new ActionBar.TabListener() {

                @Override
                public void onTabReselected(Tab arg0,
                        android.app.FragmentTransaction arg1) {

                }

                @Override
                public void onTabSelected(Tab tab,
                        android.app.FragmentTransaction ft) {
                    viewpager.setCurrentItem(tab.getPosition());

                }

                @Override
                public void onTabUnselected(Tab tab,
                        android.app.FragmentTransaction ft) {

                }
            };


                    Tab tab = actionabar.newTab().setText(getString(R.string.str_home))
                            .setTabListener(tabListener);
                    actionabar.addTab(tab);
                    tab = actionabar.newTab().setText(getString(R.string.str_video))
                            .setTabListener(tabListener);
                    actionabar.addTab(tab);
                    tab = actionabar.newTab().setText(getString(R.string.str_gal))
                            .setTabListener(tabListener);
                    actionabar.addTab(tab);
                    tab = actionabar.newTab().setText(getString(R.string.str_announcements))
                            .setTabListener(tabListener);
                    actionabar.addTab(tab);

                    tab = actionabar.newTab()
                            .setText(getString(R.string.str_magazines))
                            .setTabListener(tabListener);
                    actionabar.addTab(tab);

                    tab = actionabar.newTab().setText(getString(R.string.str_conduct))
                            .setTabListener(tabListener);
                    actionabar.addTab(tab);  


        }
    }

I need to move a tab to the bottom of the screen.I am getting confused because I add a tabs with the help of HomeActivity.java coding not from xml layout.

I doesn't know how to move the tab to the bottom of the screen.Anybody can help me with these.Thank You.

回答1:

this link : Placing my ActionBar at the bottom

The google default action bar does not in any case appear on the bottom. As it seems others have mentioned, splitActionBarWhenNarrow only puts a subset of your ActionBar (like tabs, etc) on the bottom of the screen when the device is narrow. Unfortunately, if you want to implement an ActionBar like interface on the bottom of the screen, you'll have to implement it yourself (a quick search finds this example), as I haven't seen any Libraries to do this for you.

In all, though, I would recommend against it, as it violates the Seamlessness design principle in the design documents by breaking user expectations of where ActionBar type controls will be - An android user is ideally used to doing things a certain way, and you would need a pretty compelling reason to break that expectation.

too see this link : http://developer.android.com/guide/topics/ui/actionbar.html#SplitBar

and here is a sample : http://android-ed.blogspot.com/2011/12/using-actionbar-at-bottom-of-screen.html