gpt4 book ai didi

android - 如何从设计支持 android 在选项卡布局中嵌入字体

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:43 24 4
gpt4 key购买 nike

我想从设计支持库中将自定义字体嵌入到 tablayout 中。我尝试设置已经设置字体的自定义 View 。但它不起作用。就出现系统字体。

我做的是,

mAdpater = new CustomPagerAdapter(getSupportFragmentManager());
mFragmentPager.setAdapter(mAdpater);
mTabs.setupWithViewPager(mFragmentPager);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
for (int i = 0; i < mTabs.getTabCount(); i++) {
TextView tv = new TextView(this);
tv.setText(mAdpater.getPageTitle(i));
tv.setTypeface(tf);
mTabs.getTabAt(i).setCustomView(tv);
}

欢迎提出任何建议。

最佳答案

如果您在 FragmentPagerAdapter 中覆盖 getPageTitle,则调用

mTabs.setupWithViewPager(mFragmentPager);

标签文本是为您设置的。

然后要更改字体,我使用此方法来自 this所以发布

 Typeface tf = Typeface.createFromAsset(getAssets(), "myfont.ttf");
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTypeface(tf);
}
}
}

这里是完整的FragmentPagerAdapter,供引用

class PagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public PagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

@Override
public int getCount() {
return fragmentList.size();
}

@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}

关于android - 如何从设计支持 android 在选项卡布局中嵌入字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31857952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com