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 ...
随机推荐
- JDBC:数据库操作:BLOB数据处理
CLOB主要保存海量文字,而BLOB是专门保存二进制数据:包括,图片,音乐,影片.等. 在MYSQL中,BLOB类型使用LONGBLOB声明,最高可存储4G内容. 创建一个表: create tabl ...
- Loadrunner socket协议lrs_receive函数接收到返回数据包 仍然等待服务器返回--解决
前段时间在使用loadrunner socket协议发送数据包到到服务器,使用lrs_receive接收服务器应答数据包,已经接收到数据包,但LR仍然在等待服务器端返回,而且日志打印显示每次接收返回都 ...
- Android源代码装饰模式---ContextWrapper
假设说Android源代码中哪个地方装饰模式应用的最明显的话,那肯定是非ContextWrapper莫属了,ContextWrapper是一个透明的经典的装饰模式.本文将通过装饰器模式分析Contex ...
- Idea golang "can’t find import" 解决方法
如题,在使用leveldb go wrapper levigo 的时候,本地安装好levigo后,通过命令行编译代码正常并能work,但在Idea中使用时出现: can't find import & ...
- thinkphp 3.2多语言设置
1.将CheckLangBehavior.class.php(没有的话去下载完整版)文件放到此目录下:\ThinkPHP\Extend\Behavior 2.修改目录下文件Application\Ho ...
- HTTP 状态码总结 (HTTP Status Codes)
今天与同事聊天中提及到HTTP状态码的问题,突然发现工作这么些年对这个天天打交道的东西也没有一个详细的了解.日常最常见的状态码莫过于500和404了,几乎从事IT的应该都知道或许不从事的都知道,呵呵! ...
- unity, 不要用TextMesh,用图片代替
<方块鸭快跑>(见:http://www.cnblogs.com/wantnon/p/4596222.html)1.0版本开始界面中鸭子的speech bubble中的文字用的是TextM ...
- SignalR IOS Android
http://www.dotblogs.com.tw/toysboy21/archive/2014/03/24/144505.aspx https://www.youtube.com/watch?v= ...
- 使用NoSQL Manager for MongoDBclient连接mongodb
1.安装NoSQL Manager for MongoDB 下载地址:http://www.mongodbmanager.com/download 2.打开client,选server-new mon ...
- CvSplit
/* possible split in the tree */ typedef struct CvSplit { CvTreeCascadeNode* parent; CvTreeCascadeNo ...