自定义TabLayout
总布局
实现的是底端TabLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vp_content_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tl_tab_home"/>
<android.support.design.widget.TabLayout
android:id="@+id/tl_tab_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
app:tabIndicatorHeight="0dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/darkgreen"
android:layout_alignParentBottom="true"
android:elevation="@dimen/elevation_5"/>
</RelativeLayout>
TabLayout自定义布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/tab_icon"
android:layout_width="@dimen/back_size"
android:layout_height="@dimen/back_size"/>
<TextView
android:id="@+id/tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/selector_text_tab"
android:textSize="@dimen/text_size_15"
android:enabled="false"/>
</LinearLayout>
Selector
color
<item android:state_selected="true" android:color="@color/darkgreen"></item>
<item android:color="@color/darkgray"></item>
<item android:state_pressed="true" android:color="@color/darkgreen"/>
<item android:state_enabled="true" android:color="@color/darkgreen"/>
icon
因为三个图标的selector是一样的,所以就取了一个
<item android:state_selected="true" android:drawable="@drawable/icon_dynamics_select"></item>
<item android:drawable="@drawable/icon_dynamics_unselect"></item>
<item android:state_pressed="true" android:drawable="@drawable/icon_dynamics_select"/>
代码
主要分以下两步实现,若想看怎么自定义,请直接跳到最后的getTabView
initContent();
initTab();
String[] title = {"个人","推荐","动态"};
ContentPagerAdapter
class ContentPagerAdapter extends FragmentPagerAdapter {
public ContentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return tabFragments.get(position);
}
@Override
public int getCount() {
return tabIndicators.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabIndicators.get(position);
}
}
initContent
private void initContent(){
tabIndicators = new ArrayList<>();
for(int i = 0; i < title.length; i++){
tabIndicators.add(title[i]);
}
tabFragments = new ArrayList<>();
tabFragments.add(new UserFragment());
tabFragments.add(new RecommendFragment());
tabFragments.add(new DynamicsFragment());
contentAdapter = new ContentPagerAdapter(getSupportFragmentManager());
vp_home.setAdapter(contentAdapter);
}
initTab
private void initTab(){
tab_home.setTabMode(TabLayout.MODE_FIXED);
tab_home.setSelectedTabIndicatorHeight(0);
ViewCompat.setElevation(tab_home,getResources().getDimension(R.dimen.elevation_5));
tab_home.setupWithViewPager(vp_home);
for(int i = 0; i < title.length;i++){
TabLayout.Tab tab = tab_home.getTabAt(i);
if(tab != null){
tab.setCustomView(getTabView(i));
}
}
tab_home.getTabAt(1).select();
}
getTabView
private View getTabView(int position){
TypedArray prodIcon_tab = getResources().obtainTypedArray
(R.array.prodIcon_tab);//图片资源ID数组
View view = LayoutInflater.from(this).inflate(R.layout.tab_item,null);
TextView tab_text = (TextView)view.findViewById(R.id.tab_text);
tab_text.setText(title[position]);
ImageView tab_icon = (ImageView)view.findViewById(R.id.tab_icon);
tab_icon.setImageResource(prodIcon_tab.getResourceId(position,0));
return view;
}