常用属性
1.tabMode 选项卡模式。scrollable:可滚动的选项卡。适用于选项卡过多,或者不固定的情况。
2.tabIndicatorColor 选项卡下滑线的颜色。
3.tabIndicatorHeight 选项卡下划线的高度。设置为0dp相当于隐藏。
4.tabSelectedTextColor 选项卡被选中时,字体的颜色
5.tabTextColor 选项卡未被选中时的字体的颜色
如何添加选项卡(注意:position是从0开始)
选项卡的添加常用的有:
tableLayout.addTab(Tab tab); 默认第一个选中,添加的顺序从0开始依次增加。
tableLayout.addTab(Tab tab,int position); //position要添加的位置。
tableLayout.addTab(Tab tab,boolean setSelected);
tableLayout.addTab(Tab tab,int position,boolean setSelected); setSelected设置是否选中
public classpublic class TableLayoutActivity extends AppCompatActivity { TableLayoutActivity extends AppCompatActivity { private TabLayout tableLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_table_layout); tableLayout = (TabLayout) findViewById(R.id.tableLayout); tableLayout.addTab(tableLayout.newTab().setText("选项1")); tableLayout.addTab(tableLayout.newTab().setText("选项2")); tableLayout.addTab(tableLayout.newTab().setText("选项3")); tableLayout.addTab(tableLayout.newTab().setText("选项4")); tableLayout.addTab(tableLayout.newTab().setText("选项5")); tableLayout.addTab(tableLayout.newTab().setText("add"),3); } } 结果是:选项1 选项2 选项3 add 选项4 选项5(选项1被选中 public class TableLayoutActivity extends AppCompatActivity { private TabLayout tableLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_table_layout); tableLayout = (TabLayout) findViewById(R.id.tableLayout); tableLayout.addTab(tableLayout.newTab().setText("选项1"), 0, false); tableLayout.addTab(tableLayout.newTab().setText("选项2"), 1, true); tableLayout.addTab(tableLayout.newTab().setText("选项3"), 2, false); tableLayout.addTab(tableLayout.newTab().setText("选项4"), 3, false); tableLayout.addTab(tableLayout.newTab().setText("选项5"), 4, false); tableLayout.addTab(tableLayout.newTab().setText("选项6"), 5, false); tableLayout.addTab(tableLayout.newTab().setText("选项7"), 6, false); tableLayout.addTab(tableLayout.newTab().setText("选项8"), 7, false); tableLayout.addTab(tableLayout.newTab().setText("选项9"), 8, false); tableLayout.addTab(tableLayout.newTab().setText("选项10"), 9, false); tableLayout.addTab(tableLayout.newTab().setText("选项11"), 10, false); } }