Material Design ( 3 ) - TabLayout

首发于我的博客,转载请注明作者和原文链接。

TabLayout用以实现Android上常见的页面切换效果。

属性说明

  • 下方滚动的下划线颜色
app:tabIndicatorColor
setSelectedTabIndicatorColor 
  • tab被选中后,文字的颜色
app:tabSelectedTextColor
setTabTextColors  
  • tab默认的文字颜色
app:tabTextColor
setTabTextColors  
  • tab的背景
app:tabBackground
android:background 
  • 排列模式,可选:[ fixed | scrollable ]
app:tabMode
setTabMode
  • 对齐方式,可选:[ fill | center ]
app:tabGravity
setTabGravity

XML布局:activity_toolbar.xml



    

    


Java:TabLayoutActivity

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tablayout);

    TabFragment tfAndroid = TabFragment.newInstance("Android");
    TabFragment tfIos = TabFragment.newInstance("iOS");
    TabFragment tfWeb = TabFragment.newInstance("WEB");

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    TabFragmentPagerAdapter tabFragmentPagerAdapter =
            new TabFragmentPagerAdapter(getSupportFragmentManager());
    tabFragmentPagerAdapter.add(tfAndroid, "Android");
    tabFragmentPagerAdapter.add(tfIos, "iOS");
    tabFragmentPagerAdapter.add(tfWeb, "WEB");

    viewPager.setAdapter(tabFragmentPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);
}

public static class TabFragmentPagerAdapter extends FragmentPagerAdapter {

    private List fragmentList = new ArrayList<>(); //fragment列表
    private List titleList = new ArrayList<>(); //tab名的列表

    public TabFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void add(Fragment fragment, String title) {
        fragmentList.add(fragment);
        titleList.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

public static class TabFragment extends Fragment {

    private String title;

    public static TabFragment newInstance(String title) {
        TabFragment tabFragment = new TabFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        tabFragment.setArguments(args);
        return tabFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title = getArguments().getString("title");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        TextView tv = new TextView(container.getContext());
        tv.setText(title);

        return tv;
    }
}
  • 在继承FragmentPagerAdapter时,会重写getPageTitle()方法,返回当前页面的标题。

  • TabLayout.setupWithViewPager()绑定ViewPager和TabLayout。

另外

大多数时候,这个应该都会跟AppBarLayout配合一起使用:


    

  • android:theme="@style/AppTheme.AppBarOverlay"上一篇提过,是定义在styles.xml中的。
    设置之后,背景颜色就会变成styles.xml中的colorPrimary颜色,字体颜色就会变成白色,非常方便。

你可能感兴趣的:(Material Design ( 3 ) - TabLayout)