记录一个简单的自定义TabLayout,可自定义tab背景,tab选中字体颜色、大小。
TabLayout代码:
public class HomeTabLayout extends TabLayout {
public HomeTabLayout(Context context) {
super(context);
initTabLayout();
}
public HomeTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initTabLayout();
}
public HomeTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initTabLayout();
}
private void initTabLayout() {
this.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(Tab tab) {
if (tab.getCustomView() != null) {
TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
ImageView tab_iv = tab.getCustomView().findViewById(R.id.tab_iv);
tab_text.setTextColor(UIHelper.getColor(R.color.text_color_1));
tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
tab_text.setVisibility(VISIBLE);
tab_iv.setVisibility(VISIBLE);
}
}
@Override
public void onTabUnselected(Tab tab) {
if (tab.getCustomView() != null) {
TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
ImageView tab_iv = tab.getCustomView().findViewById(R.id.tab_iv);
tab_text.setTextColor(UIHelper.getColor(R.color.text_color_2));
tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
tab_text.setVisibility(VISIBLE);
tab_iv.setVisibility(INVISIBLE);
}
}
@Override
public void onTabReselected(Tab tab) {
}
});
}
public void bindTitle(List<String> titles) {
for (int i = 0, size = titles.size(); i < size; i++) {
Tab tab = getTabAt(i);
tab.setCustomView(R.layout.hometablayout_item);
TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
tab_text.setTextColor(UIHelper.getColor(R.color.text_color_2));
tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
tab_text.setText(titles.get(i));
//设置选中
if (i == 1)
tab.select();
// if (i == 1)
// this.addTab(tab, true);
// else
// this.addTab(tab, false);
}
}
}
布局中使用:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.***********.myapplication.HomeTabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#FFFFFF"
app:tabBackground="@color/transparency"
app:tabIndicatorHeight="0dp"
app:tabMode="scrollable"
app:tabRippleColor="@color/transparency"
app:tabSelectedTextColor="@color/text_color_1"
app:tabTextColor="@color/text_color_2" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity 中操作:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HomeTabLayout tabLayout = findViewById(R.id.tab_layout);
ViewPager viewPager = findViewById(R.id.viewPager);
List<String> mTilte = new ArrayList<>();
List<Fragment> fragments = new ArrayList<>();
FragmentOne fragmentOne = new FragmentOne();
mTilte.add(UIHelper.getString(R.string.home_tab_one));
fragments.add(fragmentOne);
FragmentTwo fragmentTwo = new FragmentTwo();
mTilte.add(UIHelper.getString(R.string.home_tab_two));
fragments.add(fragmentTwo);
FragmentThree fragmentThree = new FragmentThree();
mTilte.add(UIHelper.getString(R.string.home_tab_three));
fragments.add(fragmentThree);
TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(4);
tabLayout.setupWithViewPager(viewPager);
tabLayout.bindTitle(mTilte);
}
class TabPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
TabPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
}
}
UIHelper为简单的工具类。