安卓Design包之TabLayout控件的使用
转自:
安卓Design包之TabLayout控件的简单使用
Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个support库里面,Google给我们提供了更加规范的MD设计风格的控件。最重要的是,Android Design Support Library的兼容性更广,直接可以向下兼容到Android 2.2。这不得不说是一个良心之作。
使用方法很简单,只需要添加一句依赖
compile 'com.android.support:design:24.0.0'
首先带来的是TabLayout
Tab滑动切换View并不是一个新的概念,但是Google却是第一次在support库中提供了完整的支持,
而且,Design library的TabLayout 既实现了固定的选项卡 - view的宽度平均分配,
也实现了可滚动的选项卡 - view宽度不固定同时可以横向滚动。选项卡可以在程序中动态添加,
但大部分时间我们都不会这样用,通常滑动布局都会和ViewPager配合起来使用,所以,我们需要ViewPager来帮忙:
通过一句话setupWithViewPager,我们就把ViewPager和TabLayout结合了起来。
layout_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fanggao.qf.tablelayouttest.MainActivity"> <FrameLayout
android:id="@+id/fl_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"> </FrameLayout>
</RelativeLayout>
layout_fragmentxml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tl_title"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#ffffff"
app:tabIndicatorColor="#0000ff"
app:tabTextColor="#000000"
app:tabSelectedTextColor="#0000ff"
app:tabMode="fixed">
<!-- app:tableIndicatorColor = "#0000ff" 设置下划线的颜色
app:tabTextColor="#000000" 设置文本颜色
app:tabSelectedTextColor="#0000ff" 设置选择时文本颜色
app:tabMode="scrollable" 模式:scrollable横向滚动 fixed 填充,不能滚动
-->
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout>
MainActivity:
添加碎片文件
public class MainActivity extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载fragment
getSupportFragmentManager().beginTransaction().add(R.id.fl_layout,new MainFragment()).commit(); }
}
MainFragment:
设置显示内容(添加了tabLayout与viewPager)
public class MainFragment extends Fragment {
private TabLayout tbTitle;
private ViewPager vpFragments; @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_main, container,false);
tbTitle = (TabLayout) inflate.findViewById(R.id.tl_title);
vpFragments = (ViewPager) inflate.findViewById(R.id.vp_fragment);
initView(); return inflate;
} private void initView() {
//碎片集合
ArrayList<Fragment> fragmentList = new ArrayList<>();
//标题集合
ArrayList<String> titles = new ArrayList<>();
String[] titleRes = new String[]{"推荐","排行","歌单","电台","MV"};
for (int j = 0; j < titleRes.length; j++) {
titles.add(titleRes[j]);
}
for (int i = 0; i < titleRes.length; i++) {
TestFragment testFragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putString("VALUE",titleRes[i]);
testFragment.setArguments(bundle);
fragmentList.add(testFragment);
}
//创建并设置适配器
TestFragmentAdapter testFragmentAdapter = new TestFragmentAdapter(getActivity().getSupportFragmentManager(), fragmentList, titles);
vpFragments.setAdapter(testFragmentAdapter); //将Tablelayout与viewPager绑定
tbTitle.setupWithViewPager(vpFragments);
}
}
TestFragmentAdapter:
public class TestFragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String>titles;
public TestFragmentAdapter(FragmentManager fm, List<Fragment> fragmentList,List<String>titles) {
super(fm);
this.fragmentList = fragmentList;
this.titles = titles;
} @Override
public Fragment getItem(int position) {
return fragmentList.get(position);
} @Override
public int getCount() {
return fragmentList.size();
} /**
* 将标题头与viewPager绑定
* @param position
* @return
*/
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
TestFragment(添加到viewPager中显示内容的碎片)
public class TestFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_test1, container, false);
Bundle bundle = getArguments();
String value = bundle.getString("VALUE");
Log.i("tag", "onCreateView: "+value);
TextView tvText = (TextView) inflate.findViewById(R.id.tv_textFragment);
tvText.setText(value);
return inflate; }
}
效果:
安卓Design包之TabLayout控件的使用的更多相关文章
- 安卓Design包之TabLayout控件的简单使用
Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个supp ...
- 安卓Design包之Toolbar控件的使用
转自:ToolBar的使用 ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话, ...
- 安卓Design包之超强控件CoordinatorLayout与SnackBar的简单使用
在前面的Design中,学习使用了TabLayout,NavigationView与DrawerLayout实现的神奇效果,今天就带来本次Design包中我认为最有意义的控件CoordinatorLa ...
- 安卓Design包之CollapsingToolbarLayout(可折叠的工具栏布局)的简单使用
转自: CollapsingToolbarLayout的使用 注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头 CollapsingToolbarLayout作用是提供了一个 ...
- 安卓Design包之CoordinatorLayout配合AppBarLayout,ToolBar,TabLaout的使用
转载: CoordinatorLayout配合AppBarLayout,Toolbar和TabLayout的使用 控件的简单介绍: AppBarLayout:它是继承LinerLayout实现的一个V ...
- 安卓Design包之NavigationView结合DrawerLayout,toolbar的使用,FloatingActionButton
注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头 FloatingActionButton 悬浮按钮:FloatingActionButton是重写ImageView的,所有 ...
- 【转】Appium基于安卓的各种FindElement的控件定位方法实践
原文地址:http://blog.csdn.net/zhubaitian/article/details/39754041#t11 AppiumDriver的各种findElement方法的尝试,尝试 ...
- Appium基于安卓的各种FindElement的控件定位方法实践和建议
AppiumDriver的各种findElement方法的尝试,尝试的目标应用是SDK自带的Notepad应用. 1. findElementByName 1.1 示例 el = driver.fin ...
- android开发游记:meterial design 5.0 开源控件整套合集 及使用demo
android 的5.0公布不光google官方给出了一些新控件,同一时候还给出了一套符合material design风格的设计标准,这套标准将未来将覆盖google全部产品包括pc端,站点,移动端 ...
随机推荐
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程:简介及目录》(附上完整工程文件)
介绍:讲述如何使用Genesis-3D来制作一个横版格斗游戏,涉及如何制作连招系统,如何使用包围盒实现碰撞检测,软键盘的制作,场景切换,技能读表,简单怪物AI等等,并为您提供这个框架的全套资源,源码以 ...
- 王家林 Spark公开课大讲坛第一期:Spark把云计算大数据速度提高100倍以上
王家林 Spark公开课大讲坛第一期:Spark把云计算大数据速度提高100倍以上 http://edu.51cto.com/lesson/id-30815.html Spark实战高手之路 系列书籍 ...
- node-mysql中的连接池代码学习
node-mysql是一个node.js下的mysql驱动,前段时间在处理连接池的问题上遇到了连接不释放的疑难杂症,虽已解决,但仍需总结经验避免下次重蹈覆辙.下面是node-mysql中的连接池的部分 ...
- Eclipse与tomcat服务器建立关联
首先,点击 打开preference,打开如下界面 点击ADD,进入如下界面,选择tomcat服务器的版本->点击next 进入如下界面,Name:服务器名字,directory:服务器目录 补 ...
- 一个通用的Makefile (转)
据http://bbs.chinaunix.net/thread-2300778-1-1.html的讨论,发现还是有很多人在问通用Makefile的问题,这里做一个总结.也作为以后的参考. ...
- Cocos2dx游戏源码合集(BY懒骨头+持续更新+2014.02.21)
转自:http://blog.csdn.net/iamlazybone/article/details/19612941 声明: <萝莉快跑><喵汪大战>两个demo的原作者b ...
- 【现代程序设计】【期末作业】【homework-09】
作业要求说明: http://www.cnblogs.com/xinz/p/3441537.html 我在做一个什么样的应用: 展示如何逐步求解一个加权矩阵的 最大子矩阵 任意联通图形 下面是软件的截 ...
- ubuntu 备份安装程序列表
一般情况下,我们重装ubuntu的系统会做如下几个事情 1)修改默认的程序更新源 2)开始根据需求安装软件. 3)配置文件(如vim/tmux等) 对于步骤,只需要cp /etc//etc/apt/s ...
- poj 3041 Asteroids(最小点覆盖)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- nyoj 10 skiing(记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...