TabLayout

来源:互联网 发布:paparecipe防伪软件 编辑:程序博客网 时间:2024/06/18 09:34

TableLayout

<android.support.design.widget.TabLayout    android:id="@+id/tableLayout"    android:layout_width="match_parent"    android:layout_height="wrap_content"    app:tabIndicatorColor="@color/ui_main_color"    app:tabIndicatorHeight="1dp"    app:tabMode="scrollable"    app:tabSelectedTextColor="@color/ui_main_color"    app:tabTextColor="#000000"    />

常用属性

1.tabMode 选项卡模式。scrollable:可滚动的选项卡。适用于选项卡过多,或者不固定的情况。

2.tabIndicatorColor 选项卡下滑线的颜色。

3.tabIndicatorHeight 选项卡下划线的高度。设置为0dp相当于隐藏。

4.tabSelectedTextColor 选项卡被选中时,字体的颜色

5.tabTextColor 选项卡未被选中时的字体的颜色

如何添加选项卡(注意:position是从0开始)

选项卡的添加常用的有:

  1. tableLayout.addTab(Tab tab); 默认第一个选中,添加的顺序从0开始依次增加。

  2. tableLayout.addTab(Tab tab,int position); //position要添加的位置。

  3. tableLayout.addTab(Tab tab,boolean setSelected);

  4. 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);
    }
}
原创粉丝点击