本文实例讲述了Android TabLayout(选项卡布局)简单用法。分享给大家供大家参考,具体如下:

我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合。达到很漂亮的效果。但是TabPageIndicator是第三方的,而且比较老了,当然了现在很多大神都已经开始自己写TabPageIndicator来满足自己的需求,在2015年的google大会上,google发布了新的Android Support Design库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成TabPageIndicator的效果,而且还是官方的,最好的是它可以兼容到2.2以上版本,包括2.2。下面我就举一个简单的例子来使用它。

这里使用的 android studio进行开发的,所以引用TabLayout很简单,只要在build.gradle中加入compile 'com.android.support:design:22.2.0'即可。

这个使用是我在仿 知乎 的时候使用。所以页面就和知乎很像了

fragment_find.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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/tab_FindFragment_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/titleBlue"
    app:tabIndicatorColor="@color/white"
    app:tabSelectedTextColor="@color/gray"
    app:tabTextColor="@color/white"
    />
  <android.support.v4.view.ViewPager
    android:id="@+id/vp_FindFragment_pager"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />
</LinearLayout>

这里面没有什么特别的,就是添加了一个TabLayout和Viewpager作为上下的布局。其中

?
1
2
3
app:tabIndicatorColor="@color/white" // 下方滚动的下划线颜色
app:tabSelectedTextColor="@color/gray" // tab被选中后,文字的颜色
app:tabTextColor="@color/white" // tab默认的文字颜色

Find_tab_Adapter.java  它是viewpager的Adapter,因为这里面我每个栏目下,都会有一些列表,所以采用list<View>的方式,在里面切换layout不太适合,所以我采用了List<Fragment>来直接加载多个fragment

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.example.cg.myzhihu.Adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
 * Created by cg on 2015/9/26.
 */
public class Find_tab_Adapter extends FragmentPagerAdapter {
  private List<Fragment> list_fragment; //fragment列表
  private List<String> list_Title; //tab名的列表
  public Find_tab_Adapter(FragmentManager fm,List<Fragment> list_fragment,List<String> list_Title) {
    super(fm);
    this.list_fragment = list_fragment;
    this.list_Title = list_Title;
  }
  @Override
  public Fragment getItem(int position) {
    return list_fragment.get(position);
  }
  @Override
  public int getCount() {
    return list_Title.size();
  }
  //此方法用来显示tab上的名字
  @Override
  public CharSequence getPageTitle(int position) {
    return list_Title.get(position % list_Title.size());
  }
}

FindFragment.java这个的说法,全在标注里面了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.example.cg.myzhihu;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.cg.myzhihu.Adapters.Find_tab_Adapter;
import java.util.ArrayList;
import java.util.List;
/**
 * 发现页面
 */
public class FindFragment extends Fragment {
  private TabLayout tab_FindFragment_title; //定义TabLayout
  private ViewPager vp_FindFragment_pager; //定义viewPager
  private FragmentPagerAdapter fAdapter; //定义adapter
  private List<Fragment> list_fragment; //定义要装fragment的列表
  private List<String> list_title; //tab名称列表
  private Find_hotRecommendFragment hotRecommendFragment; //热门推荐fragment
  private Find_hotCollectionFragment hotCollectionFragment; //热门收藏fragment
  private Find_hotMonthFragment hotMonthFragment; //本月热榜fragment
  private Find_hotToday hotToday; //今日热榜fragment
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_find, container, false);
    initControls(view);
    return view;
  }
  /**
   * 初始化各控件
   * @param view
   */
  private void initControls(View view) {
    tab_FindFragment_title = (TabLayout)view.findViewById(R.id.tab_FindFragment_title);
    vp_FindFragment_pager = (ViewPager)view.findViewById(R.id.vp_FindFragment_pager);
    //初始化各fragment
    hotRecommendFragment = new Find_hotRecommendFragment();
    hotCollectionFragment = new Find_hotCollectionFragment();
    hotMonthFragment = new Find_hotMonthFragment();
    hotToday = new Find_hotToday();
    //将fragment装进列表中
    list_fragment = new ArrayList<>();
    list_fragment.add(hotRecommendFragment);
    list_fragment.add(hotCollectionFragment);
    list_fragment.add(hotMonthFragment);
    list_fragment.add(hotToday);
    //将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用
    list_title = new ArrayList<>();
    list_title.add("热门推荐");
    list_title.add("热门收藏");
    list_title.add("本月热榜");
    list_title.add("今日热榜");
    //设置TabLayout的模式
    tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);
    //为TabLayout添加tab名称
    tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));
    tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));
    tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(2)));
    tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(3)));
    fAdapter = new Find_tab_Adapter(getActivity().getSupportFragmentManager(),list_fragment,list_title);
    //viewpager加载adapter
    vp_FindFragment_pager.setAdapter(fAdapter);
    //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
    //TabLayout加载viewpager
    tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
    //tab_FindFragment_title.set
  }
}

效果图,不太会做成动态的:

Android TabLayout(选项卡布局)简单用法实例分析相关推荐

  1. java的setbounds_java Swing组件setBounds()简单用法实例分析

    本文实例讲述了java Swing组件setBounds()简单用法.分享给大家供大家参考,具体如下: 先看API: public void setBounds(Rectangle r) 移动组件并调 ...

  2. python装饰器实例-Python装饰器原理与简单用法实例分析

    本文实例讲述了Python装饰器原理与简单用法.分享给大家供大家参考,具体如下: 今天整理装饰器,内嵌的装饰器.让装饰器带参数等多种形式,非常复杂,让人头疼不已.但是突然间发现了装饰器的奥秘,原来如此 ...

  3. Android学习笔记 56. TabLayout 选项卡布局

    Android学习笔记 Android基础开发--布局 文章目录 Android学习笔记 Android基础开发--布局 56. TabLayout 选项卡布局 56.1 简介 56.2 TabIte ...

  4. python asyncio回调函数_python回调函数用法实例分析

    python回调函数用法实例分析 本文实例讲述了python回调函数用法.分享给大家供大家参考.具体分析如下: 软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分为三类:同步调用.回调和异步 ...

  5. python中max函数用法_Python中max函数用法实例分析

    Python中max函数用法实例分析 更新时间:2015年07月17日 15:45:09 作者:优雅先生 这篇文章主要介绍了Python中max函数用法,实例分析了Python中max函数的功能与使用 ...

  6. python中event的用法_Python编程之event对象的用法实例分析

    本文实例讲述了Python编程中event对象的用法.分享给大家供大家参考,具体如下: Python提供了Event对象用于线程间通信,它是由线程设置的信号标志,如果信号标志位为假,则线程等待直到信号 ...

  7. Android从驱动到应用开发实例分析

    Android从驱动到应用开发实例分析 1. 第一个android应用程序 Android应用一般包含一个源代码目录src.一个资源目录res.一个配置文件AndroidManifest.xml.和一 ...

  8. PHP - 回调函数概念与用法实例分析 - 学习/实践

    1.应用场景 主要用于理解回调函数的概念, 对比JavaScript中的回调函数, 更加深刻理解回调函数的本质, 以及如何高效使用~~~ 2.学习/操作 1. 文档阅读 https://www.jb5 ...

  9. Android内存泄漏的简单检查与分析方法

    导语 内存泄漏问题大约是Android开发者最烦恼的问题之一了,项目中连续遇到几个内存泄漏问题,这里简单总结下检查分析内存泄漏的一些工具与方法. 一.什么是内存泄漏? 大家都知道,java是有垃圾回收 ...

最新文章

  1. 极速理解设计模式系列【目录索引】
  2. Nginx之rewrite使用
  3. 算法 - 插入排序(C#)
  4. PMCAFF老友会,产品圈年度最具份量的聚会
  5. 转)使用C/C++扩展Python
  6. win10修改时间同步服务器,解决win10时间服务器同步问题|重置win10时间服务配置...
  7. 邮件 自动打印 linux,Linux打印文件和发送邮件
  8. php 生僻字 拼音,php 汉字转拼音 [包含20902个基本汉字+5059生僻字]
  9. 雷军现身国庆 70 周年阅兵花车!
  10. OpenStack入门到实战视频教程全集下载(罗勇老师经典教程系列)
  11. 计算机小知识140,电脑小知识140个小技巧(7)
  12. 002概率论基本公式
  13. 线性代数——正交矩阵
  14. 深度学习人脸检测与人脸识别
  15. 训练集、验证集、测试集的作用和划分比例?
  16. Testin-手机兼容性测试
  17. poi setFontFamily设置微软雅黑有问题??
  18. KindEditor富文本编辑器【图片、视频等功能的富文本编辑器】
  19. Comsol学习笔记5:如何发挥计算机的性能,并行计算
  20. Kindle免费在线文档存储及格式转换服务

热门文章

  1. 浙江财经大学java试卷_2020年浙江财经大学社会保障考研真题试卷及试题答案,管理学考研试题下载...
  2. l bfgs算法java代码_优化算法——拟牛顿法之L-BFGS算法
  3. react native连接mysql_react-native上手之环境搭建及连接模拟器
  4. C++知识点44——类的继承概述
  5. 记录一下添加查询场地坐标功能中修改判断条件和画点的大小
  6. php制作本地程序,PHP安装程序制作
  7. js---PC端滑动进度条
  8. rocketmq源码解析之name启动(一)
  9. Java基础-Java中的堆内存和离堆内存机制
  10. 第 34 章 Gnuplot