如何使用属于android.support.design.widget包的Tablayout类的自定义字体?我正在使用它来实现快速返回视图功能.
从23.2.0开始,setTabsFromPagerAdapter已被弃用,但是使用Andreyua的答案的修改版本,您可以使用setupWithViewPager.
@Override public void setupWithViewPager(ViewPager viewPager) { super.setupWithViewPager(viewPager); if (mTypeface != null) { this.removeAllTabs(); ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0); PagerAdapter adapter = viewPager.getAdapter(); for (int i = 0, count = adapter.getCount(); i < count; i++) { Tab tab = this.newTab(); this.addTab(tab.setText(adapter.getPageTitle(i))); AppCompatTextView view = (AppCompatTextView) ((ViewGroup) slidingTabStrip.getChildAt(i)).getChildAt(1); view.setTypeface(mTypeface, Typeface.NORMAL); } } }
所有功劳都归功于Andreyua的原始代码片段,并进行了少量修改.
不幸的是,我没有足够的声誉来发表评论,或者我会直接回复:)
试试这个CustomTabLayout
public class CustomTabLayout extends TabLayout { public CustomTabLayout(Context context) { super(context); } public CustomTabLayout(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) { Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Medium.ttf"); this.removeAllTabs(); ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0); for (int i = 0, count = adapter.getCount(); i < count; i++) { Tab tab = this.newTab(); this.addTab(tab.setText(adapter.getPageTitle(i))); AppCompatTextView view = (AppCompatTextView) ((ViewGroup)slidingTabStrip.getChildAt(i)).getChildAt(1); view.setTypeface(typeface, Typeface.NORMAL); } } }