<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabMaxWidth">@dimen/tab_max_width</item>
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabPaddingStart">12dp</item>
<item name="tabPaddingEnd">12dp</item>
<item name="tabBackground">?attr/selectableItemBackground</item>
<item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">?android:textColorPrimary</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
<item name="android:textColor">?android:textColorSecondary</item>
<item name="textAllCaps">true</item>
</style>
tablayout的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_tab_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_marginTop="3dp"
android:id="@+id/iv_tab_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/tv_tab_text"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/black_1"/>
</LinearLayout>
viewpager的adapter
public class TabLayoutFragmentAdapter extends FragmentPagerAdapter {
private List<String> mTabList;
private Context mContext;
private List<Fragment> mFragments;
private ImageView mTabIcon;
private TextView mTabText;
private int[] mTabImgs;
private LinearLayout mTabView;
public TabLayoutFragmentAdapter(FragmentManager fm) {
super(fm);
}
public TabLayoutFragmentAdapter(FragmentManager fm, List<String> tabList, Context context, List<Fragment> fragments, int[] tabImgs) {
super(fm);
mTabList = tabList;
mContext = context;
mFragments = fragments;
mTabImgs = tabImgs;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mTabList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTabList.get(position);
}
/**
* set the tab view
*
* @param position
* @return
*/
public View getTabView(int position) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_tab_view, null);
mTabView = (LinearLayout) view.findViewById(R.id.ll_tab_view);
mTabText = (TextView) view.findViewById(R.id.tv_tab_text);
mTabIcon = (ImageView) view.findViewById(R.id.iv_tab_icon);
mTabText.setText(mTabList.get(position));
mTabIcon.setImageResource(mTabImgs[position]);
if (0 == position) {//the default color of item home is green
mTabText.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
mTabIcon.setImageResource(R.drawable.home_fill);
}
return view;
}
}
Fragment
public class TabLayoutFragment extends Fragment implements TabLayout.OnTabSelectedListener {
private ViewPager mViewPager;
private TabLayout mTabLayout;
private TextView mTextView;
private List<String> mTabList = new ArrayList<>();
private TabLayoutFragmentAdapter mAdapter;
private int[] mTabImgs = new int[]{R.drawable.home, R.drawable.location, R.drawable.like, R.drawable.person};
private List<Fragment> mFragments = new ArrayList<>();
public static TabLayoutFragment newInstance(String s) {
TabLayoutFragment tabLayoutFragment = new TabLayoutFragment();
Bundle bundle = new Bundle();
bundle.putString(Constants.ARGS, s);
tabLayoutFragment.setArguments(bundle);
return tabLayoutFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tablayout, container, false);
mTextView = (TextView) view.findViewById(R.id.activity_text_view);
Bundle bundle = getArguments();
if (bundle != null) {
String s = bundle.getString(Constants.ARGS);
if (!TextUtils.isEmpty(s)) {
mTextView.setText(s);
}
}
mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
initTabList();
mAdapter = new TabLayoutFragmentAdapter(getChildFragmentManager(), mTabList, getActivity(), mFragments, mTabImgs);
mViewPager.setAdapter(mAdapter);
mViewPager.setCurrentItem(0);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.setTabMode(TabLayout.MODE_FIXED);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
mTabLayout.getTabAt(i).setCustomView(mAdapter.getTabView(i));
}
mTabLayout.addOnTabSelectedListener(this);
// mViewPager.setCurrentItem(0);
// mTabLayout.getTabAt(0).setIcon(R.drawable.home);
// mTabLayout.getTabAt(1).setIcon(R.drawable.location);
// mTabLayout.getTabAt(2).setIcon(R.drawable.like);
// mTabLayout.getTabAt(3).setIcon(R.drawable.person);
// setDefaultFragment();
return view;
}
@Override
public void onStart() {
super.onStart();
initFragmentList();
}
private void setDefaultFragment() {
getChildFragmentManager().beginTransaction()
.replace(R.id.sub_content, HomeFragment.newInstance(getString(R.string.item_home)))
.commit();
}
/**
* add Fragment
*/
public void initFragmentList() {
mFragments.clear();
mFragments.add(HomeFragment.newInstance(getString(R.string.item_home)));
mFragments.add(LocationFragment.newInstance(getString(R.string.item_location)));
mFragments.add(LikeFragment.newInstance(getString(R.string.item_like)));
mFragments.add(PersonFragment.newInstance(getString(R.string.item_person)));
}
/**
* init the tab list.
*/
private void initTabList() {
mTabList.clear();
mTabList.add(getString(R.string.item_home));
mTabList.add(getString(R.string.item_location));
mTabList.add(getString(R.string.item_like));
mTabList.add(getString(R.string.item_person));
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
setTabSelectedState(tab);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
setTabUnSelectedState(tab);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
private void setTabSelectedState(TabLayout.Tab tab) {
View customView = tab.getCustomView();
TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text);
ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon);
tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
String s = tabText.getText().toString();
if (getString(R.string.item_home).equals(s)) {
tabIcon.setImageResource(R.drawable.home_fill);
} else if (getString(R.string.item_location).equals(s)) {
tabIcon.setImageResource(R.drawable.location_fill);
} else if (getString(R.string.item_like).equals(s)) {
tabIcon.setImageResource(R.drawable.like_fill);
} else if (getString(R.string.item_person).equals(s)) {
tabIcon.setImageResource(R.drawable.person_fill);
}
}
private void setTabUnSelectedState(TabLayout.Tab tab) {
View customView = tab.getCustomView();
TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text);
ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon);
tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.black_1));
String s = tabText.getText().toString();
if (getString(R.string.item_home).equals(s)) {
tabIcon.setImageResource(R.drawable.home);
} else if (getString(R.string.item_location).equals(s)) {
tabIcon.setImageResource(R.drawable.location);
} else if (getString(R.string.item_like).equals(s)) {
tabIcon.setImageResource(R.drawable.like);
} else if (getString(R.string.item_person).equals(s)) {
tabIcon.setImageResource(R.drawable.person);
}
}
}
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:orientation="vertical">
<include layout="@layout/fragment_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
app:tabBackground="@color/white"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextAppearance="@style/tabTextSizeStyle"
app:tabTextColor="@color/black_1"></android.support.design.widget.TabLayout>
</LinearLayout>
</RelativeLayout>