<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
1  TabLayout tabs = (TabLayout) this.findViewById(R.id.tabs);
tabs.addTab(tabs.newTab().setText("Tab1"));
tabs.addTab(tabs.newTab().setText("Tab2"));
tabs.addTab(tabs.newTab().setText("Tab3"));
tabs.addTab(tabs.newTab().setText("Tab4"));
tabs.addTab(tabs.newTab().setText("Tab5"));

   

注:如果Tab标签特别多的话,控件是支持自动滑动的,只需要在控件加入 app:tabMode="scrollable"  这个属性即可。

以上简单两步,就可以实现如图效果了。但是这个远远不能满足我们的需要,所以接着往下看。

给以上控件添加不同的颜色。如下图效果:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
app:tabTextColor 未选中字体颜色
app:tabSelectedTextColor 选中的字体颜色
app:tabIndicatorColor 下标指示器的颜色
app:tabIndicatorHeight 下标指示器的高度
-->

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
app:tabTextColor="@android:color/black"
app:tabSelectedTextColor="@android:color/holo_red_light"
app:tabIndicatorColor="@android:color/holo_orange_light"
app:tabIndicatorHeight="5dp"

/>
</LinearLayout>

更多的使用是与ViewPager相结合来使用。

效果:Title可以左右滑动切换;同时也可以利用ViewPager左右滑动切换。

代码步骤如下:

1、MainActivity的布局文件,activity_main.xml。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
app:tabTextColor 未选中字体颜色
app:tabSelectedTextColor 选中的字体颜色
app:tabIndicatorColor 下标指示器的颜色
app:tabIndicatorHeight 下标指示器的高度
app:tabMode="scrollable" 横向滚动
-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
app:tabTextColor="@android:color/black"
app:tabSelectedTextColor="@android:color/holo_red_light"
app:tabIndicatorColor="@android:color/holo_orange_light"
app:tabIndicatorHeight="5dp"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

2、MainActivity.java

 package com.example.com.designdemo;

 import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabs = (TabLayout) this.findViewById(R.id.tabs); List<String> titles = new ArrayList<String>();
List<Fragment> fragments = new ArrayList<Fragment>();
for (int i=0; i<10; i++) {
String title = "Tab"+ (i+1);
tabs.addTab(tabs.newTab().setText(title));
titles.add(title); Fragment fragment = new TestOneFragment(title);
fragments.add(fragment);
} ViewPager viewpager = (ViewPager) this.findViewById(R.id.viewpager);
TestOneAdapter mAdapter = new TestOneAdapter(getSupportFragmentManager(), titles, fragments);
viewpager.setAdapter(mAdapter);
tabs.setupWithViewPager(viewpager);
tabs.setTabsFromPagerAdapter(mAdapter);
} }

3、新建一个TestOneFragment.java,添加ViewPager使用。

 package com.example.com.designdemo;

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class TestOneFragment extends Fragment {
public String mTitle = null; public TestOneFragment(String title) {
this.mTitle = title;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.test_one_fragment, container, false); TextView txt = (TextView) view.findViewById(R.id.txt);
txt.setText(mTitle);

return view;
}
}

4、Fragment适配器TestOneAdapter.java

 package com.example.com.designdemo;

 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List; public class TestOneAdapter extends FragmentStatePagerAdapter { List<String> titles;
List<Fragment> fragments; public TestOneAdapter(FragmentManager fm, List<String> titles, List<Fragment> fragments) {
super(fm);
this.titles = titles;
this.fragments = fragments;
} @Override
public Fragment getItem(int position) {
return fragments.get(position);
} @Override
public int getCount() {
return fragments.size();
} @Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}

 完整DEMO下载地址:http://download.csdn.net/detail/androidsj/9301597

61、常规控件(4)TabLayout-便捷实现标签的更多相关文章

  1. 介绍三个Android支持库控件:TabLayout+ViewPager+RecyclerView

    本文主要介绍如下三个Android支持库控件的配合使用: TabLayout:android.support.design.widget.TabLayout ViewPager:android.sup ...

  2. chart控件怎么使x轴标签全部显示出来

    在vs2012中使用chart控件事,x轴的标签过多,致使默认只能显示其中的一部分,如图 当然,我们可以通过设置,使得x轴标签全部显示. 首先,通过chart控件属性,找到   “ChartAreas ...

  3. 【AngularJS学习笔记】封装一些简单的控件(封装成Html标签)

    bootstrap有强大的指令系统,可以自定义一些属性,基本知识请移步:http://angularjs.cn/A00r  http://www.cnblogs.com/lvdabao/p/33916 ...

  4. 62、常规控件(5)Navigation View –美观的侧滑视图

    1.main_layout.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.andr ...

  5. QT常规控件操作备忘

    QLabel设置边框和颜色: label->setFrameShape (QFrame::Box); label->setStyleSheet("border: 1px soli ...

  6. html学习第一讲(内容html常规控件的的使用)

    <html> <head> <title> 这是网页的标题</title> </head> <body> <h2>& ...

  7. 60、常规控件(3)Snackbar-可操作的提示框,Toast升级版

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=&quo ...

  8. 59、常规控件(2)TextInputLayout-让EditText提示更加人性化

    提示语用在显示. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...

  9. 58、常规控件(1)Floating Action Button-浮动的圆形按钮

               <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

随机推荐

  1. Ant + ivy的安装

    有了Ivy的帮忙,我们不需要为了一个库依赖管理而舍弃Ant去学那个难搞的Maven了.  基本配置步骤如下:  1.copy Ivy插件到ant_home/lib下: ivy安装 简单的安装方法: 直 ...

  2. Lintcode---验证二叉查找树

    给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. 一个节点的 ...

  3. JavaScript正则表达式基础知识汇总

    一.创建正则对象: 1.构造函数RegExp创建正则对象 var pattern = new RegExp('s$'); //pattern匹配以s结尾的字符串 2.使用正则直接量 var patte ...

  4. javascript和html中unicode编码和字符转义的详解

    1.html中的转义:在html中如果遇到转义字符(如“ ”),不管你的页面字符编码是utf-8亦或者是GB2312,都会直接打印成相应的字符:而当遇到(如:“\u8981”[此处的8981是16进制 ...

  5. unity, read text file

    using System.IO; //test read txt        //Resources.Load(...) loads an asset stored at path in a Res ...

  6. hdu 1711 Number Sequence KMP 基础题

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. mysql only_full_group_by问题

    我的mysql出现了only_full_group_by问题,网上一堆处理方案! 主要两种 一种修改配置表my.ini 另一种通过指令,屏蔽当前链接的only_full_group_by报错!我想永久 ...

  8. ruby之各种概念

    一.引言 刚开始接触ruby,遇到问题于是上网查资料,但是有时候却又看不懂,这很大一部分原因是我不知道一些关于ruby的概念名词是什么意思,所以看了别人的回答也理解不了. 二.各种名词 ruby:这个 ...

  9. (转)LUA正则表达式不完全指南

    转自剑侠论坛,并稍微修改个别文字. 好不容易闲下来,研究了一下正则表达式,然后越钻越深,经过跟大神们讨论学习后,就没有然后了.总之╮(╯▽╰)╭很有用的一个东西,至少对于用户输入的读取方面会比较方便, ...

  10. JavaScript绘图类 (DIV绘图)

    主要的图形算法抄自一个叫w_jsGraphics.js的类库,第一次看到那个库的时候,感觉那是十分神奇的存在.不过估计现在那个库早就已经找不到了. 这是很早之前的一个DIV绘图类,那时候VML+SVG ...