一、导依赖
compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
二、在Macitivity的Xml中
复制即可,但有的地方会报红,报红后先提成到RelativeLayout中,而后CTRL、ALT+空格键,选择第一个!
<<span style="color:#000080;font-weight:bold;">RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tl="http://schemas.android.com/apk/res-auto"
tools:context="com.example.asus.androidfire.MainActivity">
<<span style="color:#000080;font-weight:bold;">android.support.v4.view.ViewPager
android:id="@+id/vp_2"
android:layout_above="@+id/tl_2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<<span style="color:#000080;font-weight:bold;">com.flyco.tablayout.CommonTabLayout
android:id="@+id/tl_2"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_alignParentBottom="true"
android:background="#ffffff"
tl:tl_iconHeight="23dp"
tl:tl_iconWidth="23dp"
tl:tl_indicator_color="#ffff00"
tl:tl_indicator_height="0dp"
tl:tl_textSelectColor="#ffff00"
tl:tl_textUnselectColor="#66000000"
tl:tl_textsize="12sp"
tl:tl_underline_color="#DDDDDD"
tl:tl_underline_height="1dp"/>
</<span style="color:#000080;font-weight:bold;">RelativeLayout>
三、Mactivity中
public class MainActivity extends AppCompatActivity {
private ViewPager vp_2;
private CommonTabLayout tl_2;
private ArrayList mFragments = new ArrayList<>();
//设置标题
private static final String[] mTitles = {"微信", "通讯录", "发现", "我"};
//设置选中图标
private static final int[] mIconSelectIds = {R.drawable.ic_home_selected,
R.drawable.ic_care_selected, R.drawable.ic_girl_selected, R.drawable.ic_video_selected};
//设置未选中图标
private static final int[] mIconUnselectIds = {R.drawable.ic_home_normal,
R.drawable.ic_care_normal, R.drawable.ic_girl_normal, R.drawable.ic_video_normal};
private ViewPager mViewPager;
private CommonTabLayout mTabLayout_2;
private ArrayList mTabEntities = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
for (String title : mTitles) {
mFragments.add(new BlankFragment());
}
for (int i = 0; i < mTitles.length; i++) {
mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
}
mTabLayout_2.setTabData(mTabEntities);
mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.vp_2);
mTabLayout_2 = (CommonTabLayout) findViewById(R.id.tl_2);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles[position];
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
}
四、创建一个工具类
public class TabEntity implements CustomTabEntity {
public String title;
public int selectedIcon;
public int unSelectedIcon;
public TabEntity(String title, int selectedIcon, int unSelectedIcon) {
this.title = title;
this.selectedIcon = selectedIcon;
this.unSelectedIcon = unSelectedIcon;
}
@Override
public String getTabTitle() {
return title;
}
@Override
public int getTabSelectedIcon() {
return selectedIcon;
}
@Override
public int getTabUnselectedIcon() {
return unSelectedIcon;
}
}