TabLayout的简单使用
1、添加库
compile 'com.android.support:design:25.1.0'
2、添加布局
1 <android.support.design.widget.TabLayout 2 android:id="@+id/id_tablayout" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 /> 6 7 <!-- 其他属性: 8 改变选中字体的颜色 9 app:tabSelectedTextColor="@android:color/holo_orange_light" 10 11 改变未选中字体的颜色 12 app:tabTextColor="@color/colorPrimary" 13 14 改变指示器下标的颜色 15 app:tabIndicatorColor="@android:color/holo_orange_light" 16 17 改变整个TabLayout的颜色 18 app:tabBackground="color" 19 20 设置文字的外貌 21 app:tabTextAppearance="@android:style/TextAppearance.Holo.Large" 22 23 设置指示器下标的厚度 24 app:tabIndicatorHeight="4dp" 25 26 当Tab很多时,如此设置即可滑动 27 app:tabMode="scrollable" (默认是铺满 FIXED) 28 29 设置tab显示模式 此为居中,如果是fill,则是充满 30 app:tabGravity="center" 31 32 设置最大的tab宽度 33 app:tabMaxWidth="xxdp" 34 35 最小的tab宽度 36 app:tabMinWidth="xxdp" 37 38 39 -->
注意:需要将主题设置为android:theme="@style/Theme.AppCompat"
3、findViewById实例化后,添加tab
方法一:
通过TabLayout的addTab()方法添加新构建的Tab实例到TabLayout中:
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
方法二:
通过viewPager结合使用
tabLayout.setupWithViewPager(viewPager); //将viewpager直接绑定到tablayout上即可,viewpager有几个页面就有几个tab //设置tab上的标题可以 tabLayout.getTabAt(i).setText(tabs[i]); //也可以在 FragmentPagerAdapter 上重写方法 public CharSequence getPageTitle(int position) @Override public CharSequence getPageTitle(int position) { return titleList.get(position); }