TabLayout 简单使用。
先上效果图

在使用TabLayout 之前需要导入design包。 我使用的是android studio 只要在build.gradle中加入
compile 'com.android.support:design:24.2.0' 即可。 一、首先看一下布局文件
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabIndicatorColor="@color/white"
app:tabSelectedTextColor="@color/red"
app:tabTextColor="@color/white"
>
</android.support.design.widget.TabLayout > <android.support.v4.view.ViewPager
android:id="@+id/viewpage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"> </android.support.v4.view.ViewPager> </LinearLayout>
布局很简单,一个是TabLayout用来显示文字的,一个是ViewPage 用来真是页面内容的
其中
app:tabIndicatorColor="@color/white" // 选中项下面的标线颜色
app:tabSelectedTextColor="@color/red" // 选中时标题字体颜色
app:tabTextColor="@color/white" // 标题字体默认颜色
二、 我们看看 TabLayout 的适配器,很简单
package lyf.com.tablayoutdemo; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /**
* lyf on 2016/9/24.
* Fragment 适配器
*/
public class TabAdapter extends FragmentPagerAdapter { //Fragment集合
private List<Fragment> listFragment;
//Tab名称集合
private List<String> listTitle; public TabAdapter(FragmentManager fm,List<Fragment> listF,List<String> listS) {
super(fm);
listFragment = listF;
listTitle = listS;
} @Override
public Fragment getItem(int position) {
return listFragment.get(position);
} @Override
public int getCount() {
return listFragment.size();
} @Override
public CharSequence getPageTitle(int position) {
return listTitle.get(position);
}
}
三、看看我们的主布局界面
package lyf.com.tablayoutdemo; import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager; import java.util.ArrayList;
import java.util.List; /**
* FragmentActivity 应该导入 android.support.v4.app.Fragment包
*/
public class MainActivity extends FragmentActivity { private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titleList = new ArrayList<>(); private TabLayout tabLayout;
private ViewPager viewPager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) this.findViewById(R.id.tablayout);
viewPager = (ViewPager) this.findViewById(R.id.viewpage);
initControls();
} private void initControls() { fragmentList.add(new OneFragment());
fragmentList.add(new TwoFragment());
fragmentList.add(new ThreeFragment());
fragmentList.add(new FourFragment()); titleList.add("推荐");
titleList.add("视频");
titleList.add("热点");
titleList.add("娱乐"); //添加标签选项布局样式
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); tabLayout.addTab(tabLayout.newTab().setText(titleList.get(0)));
tabLayout.addTab(tabLayout.newTab().setText(titleList.get(1)));
tabLayout.addTab(tabLayout.newTab().setText(titleList.get(2)));
tabLayout.addTab(tabLayout.newTab().setText(titleList.get(3))); //设置页面选项
TabAdapter tabAdapter = new TabAdapter(this.getSupportFragmentManager(), fragmentList, titleList);
viewPager.setAdapter(tabAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
这里面的Fragment 就不贴出来了,大家可以用来展示自己的Fragment。这样我们的Demo 就结束了。
注意:包的引用 ViewPage和Fragment 都是引用 support.V4.app包里的。
TabLayout 简单使用。的更多相关文章
- TabLayout+Fragment+ViewPager+FragmentStatePagerAdapter实现Tab标签
首先来看下实现的效果吧: 最近在项目中实现这个效果的时候.尽管自己磕磕绊绊的实现了,可是知识确实模模糊糊的,今天天气异常的冷,在加上这个知识不太熟练,实在是没有心情进行接下来的计划,干脆借着这个时间, ...
- Python爬取CSDN博客文章
0 url :http://blog.csdn.net/youyou1543724847/article/details/52818339Redis一点基础的东西目录 1.基础底层数据结构 2.win ...
- 安卓Design包之TabLayout控件的简单使用
Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个supp ...
- TabLayout和ViewPager简单实现页卡的滑动
首先需要在当前的module中的build Gradle的 dependencies中加入以下句子 compile 'com.android.support:design:23.0.1' 因为我们用到 ...
- Android Material Design:滑动指示选项卡android.support.design.widget.TabLayout的简单使用
该TabLayout的功用,简单的说,就是当用户在该TabLayout的选项卡子item中选择触摸时候,文字和下方的指示器横条滑动指示.这个功能就是以前APP开发常用的选项卡某一卡片被切换.选中时候的 ...
- 浅谈ViewPager与TabLayout的简单用法
今天介绍一下ViewPager与TabLayout的简单用法 1.准备 在一切开始之前,你懂得,先导库,老方法,在build.gradle直接添加下面这一句 implementation ...
- 简单的TabLayout+Fragment选项卡
TabLayout属性: app:tabIndicatorColor="#fff" //下方滚动的下划线颜色 app:tabIndicatorHeight="10dp& ...
- TabLayout+ViewPager的简单使用
1. build.gradle文件中加入 compile 'com.android.support:design:22.2.0' 2.写Xml文件,注意TabLayout的三个属性 app:tab ...
- TabLayout+ViewPager制作简单导航栏
先看样例,有图有真相 绑定viewpager 此处主要说明tablayout的使用方法,viewpager绑定fragment的介绍在其他文章说明 mBinding.tabsLayout.setupW ...
随机推荐
- 【NOIP2009 T3】 最佳贸易 (双向SPFA)
C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...
- $.post() 传递多个参数.
$("#button").click(function() { /获取表单中id为idname和count的文本值付给property的两个属性 var property={&qu ...
- cocos2dx 3.x tolua 分析
cocos2dx 3.x 版本已经出到3.10了,终于决定要进行引擎版本升级,c++配合lua进行游戏开发,从3.x版本开始cocos使用了新的tolua方式,由于不懂python,折腾tolua搞的 ...
- JS-学习-DOM元素尺寸和位置
一,获取元素的css大小 1.通过style内联获取元素的大小 var box = document.getElementById('box'); // 获得元素; box.style. ...
- 3数字cn域名延续数字域名火爆行情! 珍品域名 593.cn 出售
近日, 域名投资者小维放出珍藏多年的珍品域名593.cn, 据悉该域名将参加易名中国举办的数字域名专场拍卖活动,将以1元标价起拍. 域名593.cn,数字“593”谐音“我就上.吾就上.我就商 ...
- StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks 论文笔记
StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks 本文将利 ...
- 判断ie版本
(function(){ var browser=navigator.appName var b_version=navigator.appVersion var version=b_version. ...
- 总结.NET 中什么时候用 Static
静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数.当类中没有依赖对象标识的数据或行为时,就可以使用静态类.静态类成员是可独立于任何对象标识的数据和行为,即无论对象发生什么更改,这些数据和函 ...
- Character类的2个定义大小写方法以及charAt(int index)方法
API文档charAt(int index)是这样定义的: charAt(char index):Returns the char value at the specified index.在指定的索 ...
- 1.AutoMapper核心:扁平化
对象 - 对象映射的一个常见用法是获取一个复杂的对象模型,并将其展开成一个更简单的模型. 您可以采取复杂的模型,如: public class Order { private readonly ILi ...