[关闭]
@act262 2016-12-21T12:32:19.000000Z 字数 2929 阅读 1304

Android Widget TabLayout

Android


WHAT


HOW

Gradle 中以来Support Design

在布局中使用

  1. <android.support.design.widget.TabLayout
  2. android:id="@+id/tabLayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="#7f00" />
  6. <android.support.v4.view.ViewPager
  7. android:id="@+id/viewPager"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent" />
// 在宽度比较大时,fill tab充满宽度;center 居中,只包裹tab宽度
app:tabGravity="fill"
// tab 模式,fixed 均等分配tab宽度,不可以滚动;scrollable 在宽度不够情况下可以横向滚动
app:tabMode="fixed"
// tab 的背景
app:tabBackground="@android:color/darker_gray"
// 底部下划线指示器颜色
app:tabIndicatorColor="#ff0"
// 下划线指示器高度
app:tabIndicatorHeight="10dp"
app:tabPaddingBottom="0dp"
app:tabPaddingTop="0dp"
// 文字颜色
app:tabSelectedTextColor="#7ff0"
app:tabTextColor="#f00"
// tab 文字的样式
app:tabTextAppearance="@style/textAppearance"

代码中控制Tab标题,Icon或者自定义Tab布局

  1. TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
  2. tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
  3. tabLayout.addTab(tabLayout.newTab().setText("Tab2").setIcon(R.mipmap.icon));
  4. // 或者给Tab使用自定义View布局
  5. TabLayout.Tab customTab = tabLayout.newTab();
  6. TextView textView = new TextView(this);
  7. textView.setText("custom");
  8. textView.setBackgroundColor(Color.RED);
  9. textView.setTextColor(Color.BLUE);
  10. customTab.setCustomView(textView);
  11. tabLayout.addTab(customTab);

通常是和ViewPager + Fragment搭配一起使用

  1. ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
  2. // init Fragments and Titles
  3. // ...
  4. // 必须先让ViewPager设置Adapter,然后才能使用TabLayout的setupWithViewPager
  5. viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager()));
  6. tabLayout.setupWithViewPager(viewPager);
  7. // ---
  8. private String[] mTitles;
  9. private List<Fragment> mFragmentList;
  10. private class TabPagerAdapter extends FragmentPagerAdapter{
  11. public TabPagerAdapter(FragmentManager fm) {
  12. super(fm);
  13. }
  14. // 获取对应的Tab Title
  15. public CharSequence getPageTitle(int position){
  16. return mTitles[position];
  17. }
  18. // 返回对应位置的Fragment
  19. public Fragment getItem(int position) {
  20. return mFragmentList.get(position);
  21. }
  22. // 返回Tab的个数,也是ViewPager的页数
  23. public int getCount() {
  24. return mFragmentList.size();
  25. }
  26. }

和ViewPager搭配的情况下Tab 的title则是从Adapter中设置的,ViewPager将和TabLayout一起联动

TabLayoutSample


WHY

TODO:未完待续...

TabLayout extends HorizontalScrollView

底部下划线
private final SlidingTabStrip mTabStrip;

特殊用法:

TabLayout作为ViewPager的decor

对应布局写法:

  1. <!-- 直接嵌入到ViewPager中 -->
  2. <android.support.v4.view.ViewPager
  3. android:id="@+id/viewPager"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <android.support.design.widget.TabLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content" />
  9. </android.support.v4.view.ViewPager>

这里判断TabLayout如果是嵌入到ViewPager的方式,则自动调用setupWithViewPager

  1. protected void onAttachedToWindow() {
  2. super.onAttachedToWindow();
  3. if (mViewPager == null) {
  4. // If we don't have a ViewPager already, check if our parent is a ViewPager to
  5. // setup with it automatically
  6. final ViewParent vp = getParent();
  7. if (vp instanceof ViewPager) {
  8. // If we have a ViewPager parent and we've been added as part of its decor, let's
  9. // assume that we should automatically setup to display any titles
  10. setupWithViewPager((ViewPager) vp, true, true);
  11. }
  12. }
  13. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注