https://developer.android.com/reference/android/support/design/widget/TabLayout.html
前言
Google官方在14年Google I/O上推出了全新的设计语言——Material Design。一并推出了一系列实现Material Design效果的控件库——Android Design Support Library。其中,有TabLayout, NavigationView,Floating labels for editing text,Floating Action Button,Snackbar, CoordinatorLayout, CollapsingToolbarLayout等等控件。在今后的学习中,我将一一介绍它们的特点和用法。
在移动应用中切换不同场景/功能,iOS中以底部三按钮、四按钮来实现的,而在Android中,则是抽屉式菜单或左右滑动式设计的。如何实现类似Google Play应用商店式的左右滑动,这就得靠TabLayout来实现了。
<iframe id="iframe_0.03684060349260321" style="box-sizing: border-box; border-width: initial; border-style: none; width: 690px; height: 1227px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://ww3.sinaimg.cn/mw690/8942f980gw1euwhq9y0qaj20k00zkgme.jpg?_=0.9318457280667058%22%20style=%22border:none;max-width:960px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.03684060349260321',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
正文
1.获得Android Design Support Library库:
在Gradle文件中的dependency中添加'compile 'com.android.support:design:22.2.1'依赖。
2.定义布局文件:
通过使用可知,上面那些标签时通过TabLayout实现,而下面内容的变化则是ViewPager+Fragment实现的。
因此在MainActivity中:
<?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/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
/>
</LinearLayout>
Fragment:
切换ViewPager,显示不同的Fragment,在这里用一个布局相同的Fragment示例。
<?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"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
/>
</LinearLayout>
3.具体实现代码:
1)创建Fragment
public class PageFragment extends Fragment {
public static final String ARGS_PAGE = "args_page";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARGS_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARGS_PAGE);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page,container,false);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText("第"+mPage+"页");
return view;
}
}
2)适配器类
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public final int COUNT = 5;
private String[] titles = new String[]{"Tab1", "Tab2", "Tab3", "Tab4", "Tab5"};
private Context context;
public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
相信Fragment+ViewPager+FragmentPagerAdapter的组合,大家已经用得很熟悉了,在这里我就不介绍了。
3)TabLayout的使用:
根据官方文档说明,TabLayout的使用有以下两种方式:
- 通过TabLayout的addTab()方法添加新构建的Tab实例到TabLayout中:
TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
2.第二种则是使用ViewPager和TabLayout一站式管理Tab,也就是说不需要像第一种方式那样手动添加Tab:
ViewPager viewPager = ...;
TabLayout tabLayout = ...;
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
而我们TabLayout的Demo就是用得第二种方式:
//Fragment+ViewPager+FragmentViewPager组合的使用
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
this);
viewPager.setAdapter(adapter);
//TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
运行效果:
<iframe id="iframe_0.8847997219079342" style="box-sizing: border-box; border-width: initial; border-style: none; width: 432px; height: 670px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://ww2.sinaimg.cn/mw690/8942f980gw1euwrdarumug20c00im757.gif?_=0.08810342842274577%22%20style=%22border:none;max-width:960px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8847997219079342',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
效果不错,但是TabLayout中的Tab似乎没有占满屏幕的宽度。如何解决呢?
有代码和XML两种方式:
1).代码
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
2).XML布局文件
<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"
/>
<iframe id="iframe_0.7883159621261229" style="box-sizing: border-box; border-width: initial; border-style: none; width: 389px; height: 667px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://ww3.sinaimg.cn/mw690/8942f980gw1ev31toz80kg20at0ij3z3.gif?_=0.27407963213575437%22%20style=%22border:none;max-width:960px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7883159621261229',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
下面就来解释一下TabGravity和TabMode,
TabGravity:放置Tab的Gravity,有GRAVITY_CENTER 和 GRAVITY_FILL两种效果。顾名思义,一个是居中,另一个是尽可能的填充(注意,GRAVITY_FILL需要和MODE_FIXED一起使用才有效果)
TabMode:布局中Tab的行为模式(behavior mode),有两种值:MODE_FIXED 和 MODE_SCROLLABLE。
MODE_FIXED:固定tabs,并同时显示所有的tabs。
MODE_SCROLLABLE:可滚动tabs,显示一部分tabs,在这个模式下能包含长标签和大量的tabs,最好用于用户不需要直接比较tabs。
下面用代码来比较这两种模式的不同:
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
//tabs的数据集
public final int COUNT = 10;
private String[] titles = new String[]{"Tab2221", "T2", "Tb3", "Tab4", "Tab5555555555","Tab2221", "T2", "Tb3", "Tab4", "Tab5555555555"};
...
}
//1.MODE_SCROLLABLE模式
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//2.MODE_FIXED模式
tabLayout.setTabMode(TabLayout.MODE_FIXED);
1.MODE_SCROLLABLE模式
<iframe id="iframe_0.2427922176416426" style="box-sizing: border-box; border-width: initial; border-style: none; width: 389px; height: 667px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://ww1.sinaimg.cn/mw690/8942f980gw1ev31p5sbqug20at0ijq58.gif?_=0.9811948542759306%22%20style=%22border:none;max-width:960px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2427922176416426',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
2.MODE_FIXED模式
<iframe id="iframe_0.5291927256411821" style="box-sizing: border-box; border-width: initial; border-style: none; width: 389px; height: 667px;" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://ww1.sinaimg.cn/mw690/8942f980gw1ev31kfstd6g20at0ijaay.gif?_=0.6934128892093718%22%20style=%22border:none;max-width:960px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5291927256411821',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
本文为作者原创,转载请注明出处:http://www.cnblogs.com/JohnTsai/p/4715454.html
相关推荐
Android Material Design控件之TabLayout的使用,结合viewpager的简单、快速的集成方式。
Android Material Design之TabLayout + NavigationView联合使用
android material design实现demo,项目源码下载即用,多个demo,赶快下载学习吧。低版本实现material design风格
原质化设计(Material Design)中文版 完整版 Google I/O 2014 发布的 Material Design 势必将会成为统一 Android Mobile、Android Table、Desktop Chrome 等全平台设计语言规范,对从业人员意义重大
android material design 设计风格,包括多个自定义控件
Material Design控件的使用,集合了最新的一些官方控件,实现一些功能。
Android Material Design 兼容库控件使用(二)
Android Material Design的Demo(Material Design theme/RecyclerView/CardView),Android Studio项目。 所属博文:http://blog.csdn.net/books1958/article/details/41956093
Material Design中部分控件相关使用(RecycleView TabLayout等)
Android开发MaterialDesign设计风格UI示例演示集
Android Material Design设计 Android Studion开发 Toolbar实践运用 Palette调色板运用 PagerSlidingTabStrip运用范例
AndroidStudio-Tablayout滑动TAb Android Material Design控件 TabLayout的用法 ---
一个具有Material Design设计风格的代码示例,拷贝即可用。
Android Material Design Icon Generator Plugin.zip,这个插件帮助你设置你的项目的材料设计图标。
android Material Design风格自定义控件,附带源码及演示apk
简单易懂的安卓材料设计动画!欢迎下载!!!!!!!!
android开发自己总结
DrawerLayout+CoordinatorLayout+AppBarLayout+Toolbar+TabLayout+ViewPager+NavigationView+Snackbar 实现了抽屉效果 和滑动时Toolbar自动消失等效果
Material Dialogs 是一个可高度定制易用, 符合 Material Design 风格的 Dialogs, 兼容 Android API8 以上版本, 完全可替代 Android 原生控件, 比原生更加简单易用。截图:使用示例:new AlertDialogWrapper.Builder...