Design库-TabLayout

来源:互联网 发布:如何用u盘重装mac 编辑:程序博客网 时间:2024/06/06 01:58

这里写图片描述

方式一

xml

<android.support.design.widget.TabLayout    android:id="@+id/tabLayout"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    xmlns:android="http://schemas.android.com/apk/res/android"/>

代码中

tabLayout = (TabLayout)findViewById(R.id.tabLayout);        tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));        tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));        tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));

方式二

<android.support.design.widget.TabLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/tabLayout"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <android.support.design.widget.TabItem        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Tab1"/>    <android.support.design.widget.TabItem        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Tab2"/>    <android.support.design.widget.TabItem        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Tab3"/></android.support.design.widget.TabLayout>

改变TabLayout的颜色

改变选中字体的颜色

app:tabSelectedTextColor=”@android:color/holo_orange_light”

改变未选中字体的颜色

app:tabTextColor=”@color/colorPrimary”

改变指示器下标的颜色

app:tabIndicatorColor=”@android:color/holo_orange_light”

改变整个TabLayout的颜色

app:tabBackground=”xxx”

这里写图片描述

改变TabLayout内部字体大小

app:tabTextAppearance=”@android:style/TextAppearance.Holo.Large”

这里写图片描述

改变指示器下标的高度

app:tabIndicatorHeight=”4dp”

这里写图片描述

添加图标

tabLayout.addTab(tabLayout.newTab().setText(“Tab 1”).setIcon(R.mipmap.ic_launcher));

这里写图片描述

Tab的模式

app:tabMode=”scrollable”

可以滑动

这里写图片描述

加入Padding

设置Tab内部的子控件的Padding

app:tabPadding=”xxdp”
app:tabPaddingTop=”xxdp”
app:tabPaddingStart=”xxdp”
app:tabPaddingEnd=”xxdp”
app:tabPaddingBottom=”xxdp”

设置整个TabLayout的Padding:

app:paddingEnd=”xxdp”
app:paddingStart=”xxdp”

内容的显示模式

app:tabGravity=”center”

Tab的宽度限制

设置最大的tab宽度:

app:tabMaxWidth=”xxdp”

设置最小的tab宽度:

app:tabMinWidth=”xxdp”

Tab的“Margin”

TabLayout开始位置的偏移量:

app:tabContentStart=”100dp”

TabLayout的监听事件

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {            @Override            public void onTabSelected(TabLayout.Tab tab) {            }            @Override            public void onTabUnselected(TabLayout.Tab tab) {            }            @Override            public void onTabReselected(TabLayout.Tab tab) {            }        });

和ViewPager的联动

tabLayout.setupWithViewPager(Viewpager);

默认选中某项

tablayout.getTabAt(position).select();

原创粉丝点击