61、常规控件(4)TabLayout-便捷实现标签
<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-便捷实现标签的更多相关文章
- 介绍三个Android支持库控件:TabLayout+ViewPager+RecyclerView
		本文主要介绍如下三个Android支持库控件的配合使用: TabLayout:android.support.design.widget.TabLayout ViewPager:android.sup ... 
- chart控件怎么使x轴标签全部显示出来
		在vs2012中使用chart控件事,x轴的标签过多,致使默认只能显示其中的一部分,如图 当然,我们可以通过设置,使得x轴标签全部显示. 首先,通过chart控件属性,找到 “ChartAreas ... 
- 【AngularJS学习笔记】封装一些简单的控件(封装成Html标签)
		bootstrap有强大的指令系统,可以自定义一些属性,基本知识请移步:http://angularjs.cn/A00r http://www.cnblogs.com/lvdabao/p/33916 ... 
- 62、常规控件(5)Navigation View –美观的侧滑视图
		1.main_layout.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.andr ... 
- QT常规控件操作备忘
		QLabel设置边框和颜色: label->setFrameShape (QFrame::Box); label->setStyleSheet("border: 1px soli ... 
- html学习第一讲(内容html常规控件的的使用)
		<html> <head> <title> 这是网页的标题</title> </head> <body> <h2>& ... 
- 60、常规控件(3)Snackbar-可操作的提示框,Toast升级版
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=&quo ... 
- 59、常规控件(2)TextInputLayout-让EditText提示更加人性化
		提示语用在显示. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ... 
- 58、常规控件(1)Floating Action Button-浮动的圆形按钮
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ... 
随机推荐
- JMeter高速应用
			过去长期用loadrunner做性能測试.可是渐渐认为有些麻烦了: 1.仅仅能执行在windows环境下,而生产环境差点儿清一色的linux.为了在同一网段做性能或压力測试,还须要单独部署一套wind ... 
- mongodb的基本语法(一)
			一.数据库常用命令 1.Help查看命令提示 help db.help(); db.yourColl.help(); db.youColl.find().help(); rs.help(); 2.切换 ... 
- 阿里云视频服务SDK
			原文地址:https://help.aliyun.com/document_detail/51992.html?spm=5176.doc52200.6.668.Sn3AjC SDK下载 更新时间:20 ... 
- xpath的基础实例
			本文分为路径表达式和常用函数两部分,整理自火车浏览器官方教程-火车浏览器之Xpath讲解. 小提示:可以使用火狐浏览器.我用的是火狐浏览器+firebug+firepath来进行调试,调试界面是这样的 ... 
- nodejs express route 的用法
			express 中文社区:http://expressjs.jser.us/community.html nodejs express route 的用法 1. 首先是最基本的用法. 1 2 3 4 ... 
- Mutex 进程间互斥
			学习Mutex的心得,不一定对,先记录一下. 同步技术分为两大类,锁定和信号同步. 锁定分为:Lock.Monitor 信号同步分为:AutoResetEvent.ManualResetEvent.S ... 
- Wpf 自定义控件(1)
			1. 新建一个wpf工程,在工程下面新建 一个文件夹themes,在themes下新建两个资源字典文件generic.xaml和PrettySeekBar.xaml generic.xaml ... 
- Vivado 自带IP仿真问题
			可以新建一个测试工程,通过IP catalog直接生产IP核,在IP核上右键选择 Open IP Example Design 之后选择生成路径. 启动Run Simulation. 
- MEF教程
			http://www.cnblogs.com/content/archive/2013/05/31/3111156.html 
- PHPCMS 后台学习
			1.final 不能被子类改写,不可扩展2.私有不能被继承3.构造方法,第一个被调用的方法4.static访问 类名::方法名 parent::test();这里test可以不静态 m=模块名& ... 
