1、在2015年的google大会上,google发布了新的Android Support Material Design库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成TabPageIndicator的效果。使用的 android studio进行开发的,所以引用TabLayout很简单,只要在build.gradle中加入compile 'com.android.support:design:23.+'即可。

效果图:

2、编写xml的时候注意一下这三个属性

app:tabIndicatorColor="#0f0"                每个tab下方的下划线的颜色
app:tabSelectedTextColor="#00f" 被选中的tab的文本颜色
app:tabTextColor="#f00" 未被选中的tab的文本颜色

布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="#777"
app:tabIndicatorColor="#0f0"
app:tabSelectedTextColor="#00f"
app:tabTextColor="#f00"
/> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#cccc"
/> </LinearLayout>

3、创建MyFragmentOne.java文件  

package com.wangjitao.myapptest.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.wangjitao.myapptest.R; /**
* Created by wangjitao on 2016/3/7.
*/
public class MyFragmentOne extends Fragment { public MyFragmentOne(){ }; @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_one, container, false);
}
}

  

4、创建ViewPager的适配器MyAdapter.java

package com.wangjitao.myapptest.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter; import java.util.List; /**
* Created by jh on 2016/3/7.
*/
public class MyAdapter extends FragmentStatePagerAdapter {
private List<Fragment> list_Fragments ;
private List<String> list_Titles ;
public MyAdapter(FragmentManager fm ,List<Fragment> list_Fragments ,List<String> list_Titles ) {
super(fm);
this.list_Fragments = list_Fragments ;
this.list_Titles = list_Titles ;
} @Override
public Fragment getItem(int position) {
return list_Fragments.get(position);
} @Override
public int getCount() {
int rec = 0 ;
if (list_Fragments != null && list_Fragments.size() > 0){
rec = list_Fragments.size();
}
return rec;
} @Override
public CharSequence getPageTitle(int position) {
return list_Titles.get(position);
}
}

  5、MainActivity.java

package com.wangjitao.myapptest.activity;

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 android.os.Bundle; import com.wangjitao.myapptest.R;
import com.wangjitao.myapptest.adapter.MyAdapter;
import com.wangjitao.myapptest.fragment.MyFragmentOne; import java.util.ArrayList;
import java.util.List; public class MainActivity extends FragmentActivity {
public static final String TAG = "MainActivity";
private TabLayout mTabLayout ;
private ViewPager mViewPager ; private List<Fragment> list_Fragments ;
private List<String> list_Titles ; private MyAdapter mMyAdapter ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
} private void initData() {
list_Fragments = new ArrayList<>();
list_Titles = new ArrayList<>();
for(int i = 0 ; i < 4 ; i++){
list_Fragments.add(new MyFragmentOne());
}
list_Titles.add("one");
list_Titles.add("two");
list_Titles.add("three");
list_Titles.add("four"); mMyAdapter = new MyAdapter(getSupportFragmentManager(),list_Fragments,list_Titles);
mViewPager.setAdapter(mMyAdapter);
mTabLayout.setupWithViewPager(mViewPager);
} private void initView() {
mTabLayout = (TabLayout)findViewById(R.id.tablayout);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
}
}

  补充:TabLayout中的Tab似乎没有占满屏幕的宽度,怎么解决呢?

  代码:

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

  布局文件设置:

<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
app:tabGravity="fill"
app:tabMode="fixed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

  

Android 5.0新特性了解(一)----TabLayout的更多相关文章

  1. Android 6.0 新特性 整理 资料来自网络

    Android 6.0新特性 Runtime Permissions Doze and App Standby Apache HTTP Client Removal BoringSSL Access ...

  2. Android 8.0 新特性

    Android 8.0 (Android Oreo(奥利奥))新特性介绍 通知渠道 - Notification Channels 通知渠道是由应用自行定义的通知内容类别,借助渠道,开发者可以让用户对 ...

  3. android 7.0 新特性 和对开发者的影响

    android 7.0新特性 - jiabailong的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/jiabailong/article/details/5241 ...

  4. 腾讯云安全:开发者必看|Android 8.0 新特性及开发指南

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 A ...

  5. 开发者必看|Android 8.0 新特性及开发指南

    背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0.谷歌在今年3 月21日发布 Andro ...

  6. Android 7.0新特性

    还望支持个人博客站:http://www.enjoytoday.cn 由于google目前不是无法直接在国内访问,故此,对于android 开发平台的7.0新特性做个保存.也可供大家查阅.原文转自an ...

  7. android 5.0新特性

    Android Lollipop 面向开发人员的主要功能 Material Design 设计 注重性能 通知 以大屏幕呈现 以文档为中心 连接性能再上一级 高性能图形 音频处理功能更强 摄像头和视频 ...

  8. Android 6.0 新特性

    首先谈一谈Android 6.0的一些新特性 锁屏下语音搜索 指纹识别 更完整的应用权限管理 Doze电量管理 Now onTap App link 在开发过程中与我们关系最密切的就是"更完 ...

  9. Android 5.0 新特性

    Material Design Material Design简介 Material Design是谷歌新的设计语言,谷歌希望寄由此来统一各种平台上的用户体验,Material Design的特点是干 ...

  10. android 5.0新特性CardView教程

    CardView 是android5.0新加入的特性,大家先别着急,由于谷歌出了cardview的兼容包,也就是android.support.v7.widget.CardView包,所以在5.0以下 ...

随机推荐

  1. 【摘自网络】陈奕迅&&杨千嬅

    揭陈奕迅杨千嬅相爱18年恋人未满的点滴片段 文/一床情书 但凡未得到,但凡是过去,总是最登对 ——题记 已经仙逝多年的香港歌坛天后梅艳芳曾经在<似是故人来>里唱道:“但凡未得到,但凡是过去 ...

  2. Reactor 与 Proactor

    一.五种IO Model blocking IO nonblocking IO IO multiplexing signal driven IO(不常用) asynchronous IO 对于一个ne ...

  3. VS2015 多项目源码共享链接

    Eclipse有这个功能,在一个项目中加入另一个项目文件夹的引用,源码包含过来,这样不必copy一份代码,只需要维护一份源代码.一直想在VS中找到这个功能,目前项目需要,终于google到了. htt ...

  4. [转]AppCompat 22.1,Goole暴走,MD全面兼容低版本

    AppCompat 22.1,Goole暴走,MD全面兼容低版本 分类: Android2015-04-24 09:48 1354人阅读 评论(0) 收藏 举报 android   目录(?)[+] ...

  5. linux bq20z75 驱动

    新的项目中使用到了电池.电池的guage使用TI的bq20z75.kernel的驱动中已经有bq20z75的驱动,只要稍加修改就可以使用. 参考链接 http://www.ti.com/lit/er/ ...

  6. 用CocoaPods做iOS程序的依赖管理

    CocoaPods简介 每种语言发展到一个阶段,就会出现相应的依赖管理工具,例如Java语言的Maven,nodejs的npm.随着iOS开发者的增多,业界也出现了为iOS程序提供依赖管理的工具,它的 ...

  7. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  8. iOS: 悬浮的条件筛选下拉框的使用

    1.介绍 app中条件筛选视图是很常用的功能,一般它搭配着tableView的表头悬浮滚动使用,点击按钮时,就会弹出下拉框显示条件,选择一个条件后,下拉框自动隐藏. 2.效果图如下 从中间点击弹出,然 ...

  9. JAVA定时器实现之一(通过继承TimerTask)

    在某些时候, 我们需要实现这样的功能,某一程序隔一段时间执行一次,而这一事情由系统本身来完成,并不是人为的触发,我们一般可称此为定时器任务. 这类技术主要应用到那些需要进行后台整理数据的系统中,比如说 ...

  10. 微信小店开发(2) DIY货架

    微信小店的货架支持开放给开发者使用,即开发者可以将自己的页面作为货架,通过JavaScript API来调起微信客户端原生的商品详情页. 请注意:1. 开发者需要预先通过1.1中的增加商品API,预先 ...