Hey. I'm a newbie. I try the design recommended by Google.

It turned out to write tabs with text . And all that now remains for me is to change the text to icons .

tool_bar.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar android:layout_height="wrap_content" android:layout_width="match_parent" android:background="@color/ColorPrimary" android:elevation="2dp" android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" xmlns:android="http://schemas.android.com/apk/res/android" /> 

activity_main.xml

 <LinearLayout 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:orientation="vertical" tools:context=".MainActivity"> <include android:id="@+id/tool_bar" layout="@layout/tool_bar" android:layout_height="wrap_content" android:layout_width="match_parent"/> <startandroid.ru.daytwo.SlidingTabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="2dp" android:background="@color/ColorPrimary" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_weight="1"> </android.support.v4.view.ViewPager> </LinearLayout> 

MainActivity.java

 public class MainActivity extends ActionBarActivity { Toolbar toolbar; ViewPager pager; ViewPagerAdapter adapter; SlidingTabLayout tabs; CharSequence Titles[]={"Π’ΠΊΠ»Π°Π΄ΠΊΠ° 1","Π’ΠΊΠ»Π°Π΄ΠΊΠ° 2", "Π’ΠΊΠ»Π°Π΄ΠΊΠ° 3"}; int Numboftabs = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.tool_bar); setSupportActionBar(toolbar); adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(adapter); tabs = (SlidingTabLayout) findViewById(R.id.tabs); tabs.setDistributeEvenly(true); tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { @Override public int getIndicatorColor(int position) { return getResources().getColor(R.color.tabsScrollColor); } }); tabs.setViewPager(pager); } public CharSequence getPageTitle(int position) { Drawable image = getResources().getDrawable(imageResId[position]); image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight()); SpannableString sb = new SpannableString(" "); ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM); sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return sb; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } } 

ViewPagerAdapter.java

 public class ViewPagerAdapter extends FragmentStatePagerAdapter { CharSequence Titles[]; int NumbOfTabs; public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) { super(fm); this.Titles = mTitles; this.NumbOfTabs = mNumbOfTabsumb; } @Override public Fragment getItem(int position) { if(position == 0) { Tab1 tab1 = new Tab1(); return tab1; } else { Tab2 tab2 = new Tab2(); return tab2; } } @Override public CharSequence getPageTitle(int position) { return Titles[position]; } @Override public int getCount() { return NumbOfTabs; } } 

What is the best way to do this? If necessary, I can throw off SlidingTabLayout.java and SlidingTabStrip.java

    1 answer 1

    It is best to use the new TabLayout from design from Google. According to en-SO , adding icons to tabs is very simple:

     //связываСм Ρ‚Π°Π±Ρ‹ с pager-ΠΎΠΌ mTabLayout.setupWithViewPager(mViewPager); //пробСгаСмся ΠΏΠΎ Ρ‚Π°Π±Π°ΠΌ ΠΈ ставим ΠΈΠΌ ΠΈΠΊΠΎΠ½ΠΊΠΈ for (int i = 0; i < mTabLayout.getTabCount(); i++) { mTabLayout.getTabAt(i).setIcon(R.drawable.your_icon); }