简体   繁体   中英

How to make tabs in layout center in android

These code is running properly. But My Query is Tabs show on ActionBar I want to show inside the layout or below the ActionBar. Please anybody help me. Please anybody help me to show the tab inside layout.

MainActivity.java class

public class MainActivity extends FragmentActivity {
  ViewPager Tab;
    TabPagerAdapter TabAdapter;
  ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                      actionBar = getActionBar();
                      actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){
      @Override
      public void onTabReselected(android.app.ActionBar.Tab tab,
          FragmentTransaction ft) {
      }
      @Override
       public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
              Tab.setCurrentItem(tab.getPosition());
          }
      @Override
      public void onTabUnselected(android.app.ActionBar.Tab tab,
          FragmentTransaction ft) {
      }};
      //Add New Tab
      actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
    }
}

TabPagerAdapter.java class

public class TabPagerAdapter extends FragmentStatePagerAdapter {
        public TabPagerAdapter(FragmentManager fm) {
        super(fm);
      }
      @Override
      public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return new Android();
            case 1:
                return new Ios();
            case 2:
                return new Windows();
            }
        return null;
      }
      @Override
      public int getCount() {
        return 3; 
      }
    }

Ios.java, Android.java and Windows.java class are similar code:

public class Ios extends Fragment {
   @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View ios = inflater.inflate(R.layout.ios_frag, container, false);
          ((TextView)ios.findViewById(R.id.textView)).setText("iOS");
          return ios;
    }
   }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

android_frag.xml, ios_frag and windows_frag.xml are similar code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50sp"/>
</LinearLayout>

by customizing my application theme

In AndroidManifest.xml, add android:theme property:

<application
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/CustomActionBarTheme">
  ...

added in themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBar</item>
    </style>

    <style name="MyActionBarTabBar">
        <item name="android:gravity">center</item>
    </style>
</resources>

The tabs should now be center-aligned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM