tablayout 标题的自定义字体

custom typeface for tablayout title

如何在 android 中为 tablayout 标题设置自定义字体?我想在标题中使用 Yekan 字体,我尝试并搜索过但没有找到任何方法...

Java :

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("ورود"));
    tabLayout.addTab(tabLayout.newTab().setText("ثبت نام"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

Xml :

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#e7e7e7"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="30dp"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:paddingTop="30dp" />

有什么方法可以设置例如:

Typeface yekan = Typeface.createFromAsset(getAssets(), "fonts/yekan.ttf");

我的标签的字体:

tabLayout.addTab(tabLayout.newTab().setText("ورود"));
tabLayout.addTab(tabLayout.newTab().setText("ثبت نام"));

你可以试试这个

res ⇒ 布局下创建一个名为 custom_tab.xml 的 xml 布局。 在这个布局中,我们为选项卡定义了自定义视图。

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tab"
    />

现在打开Activity.java并修改代码如下。

TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("ONE");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);// if you want to set icon on that tab
Typeface yekan = Typeface.createFromAsset(getAssets(), "fonts/yekan.ttf");
tabOne.setTypeface(yekan);
tabLayout.getTabAt(0).setCustomView(tabOne);