我想在tablayout中添加一种字体。使用Json动态填充选项卡。 请建议我如何在标签标题中添加字体。
这是tablayout
private void setupPager() {
viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getContext()));
tabLayout.setupWithViewPager(viewPager);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "EdgeCaps.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);
}
}
}
}
这是适配器。这里tabnames是一个由json解析填充的字符串数组。
private class CustomAdapter extends FragmentPagerAdapter {
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new ItemsMenuFragment();
return fragment;
}
@Override
public int getCount() {
return tabNames.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabNames.get(position);
}
}
}
任何人都可以帮我更改tablayout的字体。我试图在setupPager中使用它,但它不起作用。
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "EdgeCaps.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);
}
}
}
答案 0 :(得分:2)
不完全确定您的代码中发生了什么。但是这门课可能有所帮助。只要将新选项卡添加到TabLayout,它就可以将选项卡内的所有TextView设置为EdgeCaps.tff。
使用它代替普通的TabLayout,并确保将您的EdgeCaps.tff字体放在&#34; assets / fonts&#34;项目目录。
Here's an example of where the font file is meant to go (using Android Studio).
代码已从here修改为适用于Android支持库23.3.0:
public class CustomFontTabLayout extends TabLayout {
private Typeface mCustomTypeFace;
public CustomFontTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialise();
}
public CustomFontTabLayout(Context context) {
super(context);
initialise();
}
public CustomFontTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialise();
}
private void initialise()
{
// Note: Rename "EdgeCaps.tff" to whatever your font file is named.
// Note that the font file needs to be in the assets/fonts/ folder.
mCustomTypeFace = Typeface.createFromAsset(getContext().getAssets(), "fonts/EdgeCaps.tff");
}
@Override
public void addTab(@NonNull Tab tab, boolean setSelected) {
super.addTab(tab, setSelected);
setTabTypeFace(tab);
}
@Override
public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
super.addTab(tab, position, setSelected);
setTabTypeFace(tab);
}
private void setTabTypeFace(Tab tab)
{
ViewGroup tabLayoutView = (ViewGroup)getChildAt(0);
ViewGroup tabView = (ViewGroup) tabLayoutView.getChildAt(tab.getPosition());
int tabViewChildCount = tabView.getChildCount();
for (int i = 0; i < tabViewChildCount; i++) {
View tabViewChild = tabView.getChildAt(i);
// Find the TextView in the tab
if (tabViewChild instanceof TextView) {
TextView tabTextView = (TextView)tabViewChild;
// Set the TextView's font
tabTextView .setTypeface(mCustomTypeFace, Typeface.NORMAL);
}
}
}
}
答案 1 :(得分:0)
<style name="NavigationTab" parent="Widget.Design.TabLayout">
<item name="tabBackground">@drawable/background_tab</item>
<item name="tabSelectedTextColor">@color/primary_light</item>
<item name="tabIndicatorColor">@color/blue</item>
<item name="tabTextAppearance">@style/NavigationTabTextAppeareance</item>
</style>
<style name="NavigationTabTextAppeareance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">@color/primary_light</item>
<item name="android:textSize">12sp</item>
</style>
在TabLayout xml文件中。将其添加为样式。根据你的需要改变fontsize。