通过TabLayout和ViewPager实现Tab切换

本文详细介绍了如何通过TabLayout和ViewPager在Android应用中实现Tab切换效果,包括布局文件配置、Fragment创建、适配器实现以及Activity的初始化步骤。通过示例代码展示了如何设置滑动条样式和颜色,以及创建和管理Fragment。
摘要由CSDN通过智能技术生成

一、效果图


通过TabLayout和ViewPager这两个控件可以实现Tab切换的效果。而且能通过属性设置来设定滑动条的高度和颜色、字体的颜色等。效果如图所示。


二、Android布局文件


Activity的布局代码如下:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
             
android:orientation="vertical">
   
<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_alignParentBottom="true"
           
android:background="@android:color/white"
           
app:tabIndicatorColor="?attr/colorPrimary"
           
app:tabIndicatorHeight="5dp"
           
app:tabSelectedTextColor="?attr/colorPrimary"
           
app:tabTextColor="@android:color/darker_gray"/>
   
<!--
 
      app:tabIndicatorColor="@color/colorAccent"设置指示器滑动条的颜色
       
app:tabIndicatorHeight="5dp" 设置滑动条的高度
       
app:tabSelectedTextColor="#000" 设置当前页卡的标题颜色
       
app:tabTextColor="#fff"设置页卡标题颜色
   
-->

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值