Android Design Support控件介绍之TabLayout
2016-01-27 16:36
417 查看
接着上一篇[Android Design Support Library常用控件介绍(上)](http://blog.csdn.net/true100/article/details/50593636)这篇来简单介绍下TabLayout的使用。 通常我们在做带有标题的左右滑动效果时,都使用的是TabPageIndicator+ViewPager来实现。今天我们使用TabLayout+ViewPager来实现同样的效果,而且实现方法比之前的简单得多。我们直接进入代码吧!
主界面布局
<?xml version="1.0" encoding="utf-8"?> <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:fitsSystemWindows="true" android:orientation="vertical"> <!--tabTextColor设置文字颜色--> <!--tabSelectedTextColor点中时的文字颜色--> <!--tabIndicatorColor指示条的颜色--> <!--tabIndicatorHeight指示条的高度--> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorColor="@android:color/holo_green_dark" app:tabIndicatorHeight="4dp" app:tabSelectedTextColor="@android:color/holo_red_dark" app:tabTextColor="@android:color/black"></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout>
用到的Fragment页面简单布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="10dp"> <TextView android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
Frament类
public class TestFragment extends Fragment { private String content; private TextView content_tv; private View fragmentView; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); content=(String)getArguments().get("content"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { fragmentView= inflater.inflate(R.layout.content_main,null); content_tv=(TextView)fragmentView.findViewById(R.id.content_tv); content_tv.setText(content); return fragmentView; } }
ViewPager适配器类
/** * Created by: ${ldm} * time : 2016/1/27 15:34 */ public class TestAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; private List<String>titles; public TestAdapter(FragmentManager fm,List<String>titles,List<Fragment> fragments) { super(fm); this.fragments=fragments; this.titles=titles; } /** * Return the Fragment associated with a specified position. * * @param position */ @Override public Fragment getItem(int position) { return fragments.get(position); } /** * Return the number of views available. */ @Override public int getCount() { return fragments.size(); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } }
主页面代码类
public class MainActivity extends FragmentActivity { //便捷实现标签显示 private TabLayout tab_layout; private ViewPager viewpager; private TestAdapter mAdapter; private List<Fragment> fragments; private List<String> titles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initViewPages(); } private void initViewPages() { fragments = new ArrayList<>(); titles = new ArrayList<>(); for (int i = 0; i < 4; i++) { String title="TAB" + i; titles.add(title); tab_layout.addTab(tab_layout.newTab().setText(title));//添加Tab标题 Fragment fragment = new TestFragment(); Bundle bundle = new Bundle(); bundle.putString("content", "这是第" + i + "个Fragment页面"); fragment.setArguments(bundle); fragments.add(fragment); } mAdapter = new TestAdapter(this.getSupportFragmentManager(), titles, fragments); viewpager.setAdapter(mAdapter); tab_layout.setupWithViewPager(viewpager); tab_layout.setTabsFromPagerAdapter(mAdapter); } private void initViews() { tab_layout = (TabLayout) findViewById(R.id.tab_layout); viewpager = (ViewPager) findViewById(R.id.viewpager); } }
效果如图:
相关文章推荐
- Android 仿IOS3D时间选择器城市选择器
- 从框架到完整项目搭建,实战项目《约个球》(7)- 将项目迁移到android studio,使用gradle搭建android项目(上)
- Android 使用ViewPager实现左右循环滑动图片和显示当前页码
- 获取wifi的ip地址
- 王学岗Fragment(二)
- android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态
- RecyclerView替换ListView(包含SwipeRefreshLayout)
- Android ViewPager 删除动画
- BitmapUtil处理工具类(圆角,模糊,黑白,存储指定路径)
- Android 自定义View (四) 视频音量调控
- 使应用中的字体不受系统设置影响的两种方法
- android 侧滑菜单
- Android 自定义View (三) 圆环交替 等待效果
- android不同的按钮一起点击崩溃解决
- Androidstudio gradle兼容问题
- Android 自定义View (二) 进阶
- android运行时提示:could not find *.apk
- Android 自定义View (一)
- 自定义Activity父类
- android布局属性详解