TabLayout+ViewPager的简单使用
Posted 短暂的火光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TabLayout+ViewPager的简单使用相关的知识,希望对你有一定的参考价值。
1、 build.gradle文件中加入
compile ‘com.android.support:design:22.2.0‘
2、写Xml文件,注意TabLayout的三个属性
app:tabIndicatorColor="#0f0" 每个tab下方的下划线的颜色
app:tabSelectedTextColor="#00f" 被选中的tab的文本颜色
app:tabTextColor="#f00" 未被选中的tab的文本颜色
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#777" app:tabIndicatorColor="#0f0" app:tabSelectedTextColor="#00f" app:tabTextColor="#f00" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#cccc" /> </LinearLayout> activity_main.xml
1 package com.xqx.com.tablayoutdemo; 2 3 import android.support.design.widget.TabLayout; 4 5 import android.support.v4.app.Fragment; 6 import android.support.v4.app.FragmentActivity; 7 import android.support.v4.view.ViewPager; 8 import android.os.Bundle; 9 10 import java.util.ArrayList; 11 import java.util.List; 12 13 public class MainActivity extends FragmentActivity { 14 15 private TabLayout tabLayout; 16 private ViewPager viewPager; 17 18 //四个fragment 19 private OneFragment oneFragment; 20 private TwoFragment twoFragment; 21 private ThreeFragment threeFragment; 22 private FourFragment fourFragment; 23 24 //适配器 25 private MyAdapter adapter; 26 27 private List<Fragment> fragments; //fragment集合 28 private List<String> titles; //tab标题集合 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 tabLayout = (TabLayout) findViewById(R.id.tablayout); 35 viewPager = (ViewPager) findViewById(R.id.viewpager); 36 37 //创建四个Fragment对象 38 oneFragment = new OneFragment(); 39 twoFragment = new TwoFragment(); 40 threeFragment = new ThreeFragment(); 41 fourFragment = new FourFragment(); 42 43 //将四个Fragment对象添加到集合中 44 fragments = new ArrayList<>(); 45 fragments.add(oneFragment); 46 fragments.add(twoFragment); 47 fragments.add(threeFragment); 48 fragments.add(fourFragment); 49 50 //给Tab添加标题 51 titles = new ArrayList<>(); 52 titles.add("one"); 53 titles.add("two"); 54 titles.add("three"); 55 titles.add("four"); 56 57 //创建适配器 58 adapter = new MyAdapter(getSupportFragmentManager(),fragments,titles); 59 //viewpager绑定适配器 60 viewPager.setAdapter(adapter); 61 //tabLayout绑定viewpager 62 tabLayout.setupWithViewPager(viewPager); 63 64 } 65 66 } 67 68 MainActivity.java
以上是关于TabLayout+ViewPager的简单使用的主要内容,如果未能解决你的问题,请参考以下文章
TabLayout+ViewPager+Fragment 快速实现标题切换效果
如何使 tablayout 片段适合 INSIDE viewpager
用TabLayout+ViewPager+Fragment控件实现简单的翻页效果 --Android--