TabLayout简单使用(一)

1 导入包

29以前:

implementation 'com.android.support:design:28.0.0'

29及以后(使用androidx):

implementation 'com.google.android.material:material:1.0.0'

2 常用的布局属性

2.1 tabIndicatorFullWidth

tabIndicatorFullWidth表示Tab指示器的宽度是否满宽度,即与Tab布局一样宽度。

//指示器不与Tab的宽度相同,其宽度由Tab中的文字宽度决定
app:tabIndicatorFullWidth="false"
//指示器与Tab的宽度相同,其宽度由Tab的宽度决定,默认为true
app:tabIndicatorFullWidth="true"

app:tabIndicatorFullWidth="false"如图所示:
指示器宽度

app:tabIndicatorFullWidth="true"如图所示:
指示器宽度

2.2 其它属性

属性描述
tabIndicatorColor设置tab指示器(tab的下划线)的颜色。
tabIndicatorHeight设置tab指示器(tab的下划线)的高度。
tabBackground设置的背景。
tabContentStart相对起始位置tab的Y轴偏移量。
tabGravitytab的布局方式,两个值GRAVITY_CENTER (内容中心显示) 和 GRAVITY_FILL (内容尽可能充满TabLayout)。
tabMaxWidth设置tab选项卡的最大宽度。
tabMinWidth设置tab选项卡的最小宽度。
tabMode设置布局中tab选项卡的行为模式,两个常量MODE_FIXED (固定的tab)和 MODE_SCROLLABLE(滑动的tab)。
tabPadding设置tab的内边距(上下左右)。
tabPaddingTop设置tab的上方内边距。
tabPaddingBottom设置tab的底部内边距。
tabPaddingStart设置tab的左侧内边距。
tabPaddingEnd设置tab的右侧内边距。
tabSelectedTextColor设置tab被选中时的文字颜色。
tabTextColor设置tab默认的文字颜色。
tabRippleColor设置水波纹背景颜色,如果设置为透明则可达到去掉水波纹的效果

3 TabLayout方法介绍

方法描述
addOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)添加一个TabLayout.OnTabSelectedListener监听事件,当tab选择更改时,它将被调用。
addTab(TabLayout.Tab tab,boolean setSelected)向此布局添加选项卡,并设置是否被选中
addTab(TabLayout.Tab tab,int position)向此布局添加选项卡,并设置位置
addTab(TabLayout.Tab tab)向此布局添加选项卡。
addTab(TabLayout.Tab tab,int position,boolean setSelected)向此布局添加选项卡,设置位置并是否被选中
addView(View child,int index)添加子视图到指定位置。
addView(View child)添加子视图。
addView(View child,ViewGroup.LayoutParams params)添加具有指定布局参数的子视图。
addView(View child,int index,ViewGroup.LayoutParams params)添加具有指定布局参数的子视图。
clearOnTabSelectedListeners()删除所有以前添加的TabLayout.OnTabSelectedListeners。
generateLayoutParams(AttributeSet attrs)根据提供的属性集返回一组新的布局参数。
getSelectedTabPosition()返回当前所选标签的位置。
TabLayout.Tab getTabAt(int index)返回指定位置的tab。
getTabCount()返回当前在操作栏中注册的选项卡数。
getTabGravity()返回当前的标签tab的布局方式,GRAVITY_CENTER (内容中心显示) 和 GRAVITY_FILL (内容尽可能充满TabLayout)。
getTabMode()返回tab选项卡的行为模式,MODE_FIXED* (固定的tab)和 MODE_SCROLLABLE(滑动的tab)。
getTabTextColors()获取用于选项卡的不同状态(正常,已选择)的文本颜色。
newTab ()创建并返回一个新的TabLayout.Tab。
removeAllTabs()从操作栏中删除所有选项卡,并取消选择当前选项卡。
removeOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)删除以前通过addOnTabSelectedListener(OnTabSelectedListener)添加的给定TabLayout.OnTabSelectedListener,tab选中监听器。
removeTab(TabLayout.Tab tab)从布局中删除选项卡。
removeTabAt(int position)从布局中删除指定位置的选项卡。
setOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)API方法24.0.0中已弃用此方法。使用addOnTabSelectedListener(OnTabSelectedListener)和removeOnTabSelectedListener(OnTabSelectedListener)。
setScrollPosition(int position,float positionOffset,boolean updateSelectedText)设置选项卡的滚动位置,当标签tab显示为滚动容器(如ViewPager)的一部分时,此功能非常有用。位置int:当前滚动位置positionOffset float:表示从位置偏移的[0, 1)的值。updateSelectedText boolean:是否更新文本的选择状态。
setSelectedTabIndicatorColor(int color)设置选中的tab的指示器(下划线)颜色。
setSelectedTabIndicatorHeight(int height)设置选中的tab的指示器的高度。
setTabGravity(int gravity)设置TabLayout的布局方式,GRAVITY_CENTER (内容中心显示) 和 GRAVITY_FILL (内容尽可能充满TabLayout)。
setTabMode(int mode)设置tab选项卡的行为模式,MODE_FIXED* (固定的tab)和 MODE_SCROLLABLE(滑动的tab)。
setTabTextColors(int normalColor,int selectedColor)设置用于选项卡的不同状态(常规,选定)的文字颜色。
setTabTextColors(ColorStateList textColor)设置用于选项卡的不同状态(常规,选定)的文字颜色。
setTabsFromPagerAdapter(PagerAdapter adapter)API方法23.2.0中已弃用此方法。使用setupWithViewPager(ViewPager)将TabLayout与ViewPager链接在一起。当使用该方法时,当更改PagerAdapter时,TabLayout将自动更新。
setupWithViewPager(ViewPager viewPager,boolean autoRefresh)将TabLayout与ViewPager链接在一起,当更改PagerAdapter时,TabLayout是否更新由autoRefresh决定。
setupWithViewPager(ViewPager viewPager)将TabLayout与ViewPager链接在一起。
shouldDelayChildPressedState()如果此ViewGroup的子代或子孙后代按下的状态应该被延迟,则返回true。 一般来说,应该对可以滚动的容器(如List)进行此操作。 这防止当用户实际上尝试滚动内容时出现按压状态。 由于兼容性原因,默认实现返回true。 不滚动的子类通常会覆盖此方法并返回false。

4 两种添加Tab的方式

4.1 使用代码动态添加Tab

xml布局如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/dynamic_tablayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorFullWidth="false"
        app:tabIndicatorColor="#1B67DB"
        app:tabIndicatorHeight="5dp"
        app:tabContentStart="10dp"
        app:tabGravity="fill"
        >
    </com.google.android.material.tabs.TabLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

使用代码动态添加Tab如下:

public class DynamicTabLayoutActivity extends Activity {
    private TabLayout mTabLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);
        initView();
    }

    private void initView() {
        mTabLayout = findViewById(R.id.dynamic_tablayout);

        mTabLayout.addTab(mTabLayout.newTab().setText("TAB1"));
        mTabLayout.addTab(mTabLayout.newTab().setText("TAB2TAB2TAB2"));
        mTabLayout.addTab(mTabLayout.newTab().setText("TAB3"));
        mTabLayout.addTab(mTabLayout.newTab().setText("TAB4"));

    }
}

4.2 静态添加Tab

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabIndicatorFullWidth="false"
        app:tabIndicatorColor="#1B67DB"
        app:tabIndicatorHeight="5dp"
        app:tabContentStart="10dp"
        app:tabRippleColor="#C52C2C"
        app:tabGravity="fill">
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab1"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab2Tab2Tab2"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab1"/>
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab1"/>
    </com.google.android.material.tabs.TabLayout>
</LinearLayout>

源码地址

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义 `TabLayout` 中的 `Tab` 视图,可以使用 `setCustomView` 方法。你可以为每个 `Tab` 指定一个自定义视图,这个自定义视图可以是任何你想要的布局。 以下是一个示例代码,演示如何自定义 `Tab` 的视图: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); // 创建自定义视图 View customView = getLayoutInflater().inflate(R.layout.custom_tab_layout, null); // 绑定自定义视图到 Tab 上 TabLayout.Tab tab = tabLayout.newTab().setCustomView(customView); // 添加 Tab 到 TabLayout tabLayout.addTab(tab); ``` 在上面的代码中,我们先创建了一个自定义视图 `customView`,然后将其绑定到一个新的 `Tab` 对象上,并将其添加到 `TabLayout` 中。 你需要先创建一个布局文件 `custom_tab_layout.xml`,并在其中定义你的自定义视图。例如,以下是一个简单的布局文件,其中包含一个 `TextView` 和一个 `ImageView`: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <ImageView android:id="@+id/tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_tab_icon"/> <TextView android:id="@+id/tab_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab Title"/> </LinearLayout> ``` 你可以根据自己的需求修改布局文件,包括添加更多的视图元素,更改颜色和样式等。 最后,你需要在代码中获取自定义视图中的视图元素,并为它们设置相应的属性。例如,在上面的布局文件中,我们有一个 `TextView` 和一个 `ImageView`,我们可以使用以下代码获取它们并设置它们的属性: ```java View customView = getLayoutInflater().inflate(R.layout.custom_tab_layout, null); // 获取 TextView 和 ImageView TextView tabTitle = customView.findViewById(R.id.tab_title); ImageView tabIcon = customView.findViewById(R.id.tab_icon); // 设置文本和图标 tabTitle.setText("Custom Tab Title"); tabIcon.setImageResource(R.drawable.ic_custom_tab_icon); ``` 这样,我们就可以自定义 `TabLayout` 中的 `Tab` 视图了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值