Java TabLayout字体可以';不能改变

Java TabLayout字体可以';不能改变,java,android,xml,Java,Android,Xml,大家好,我正在开发一个选项卡式应用程序,其中我为应用程序徽标和带有选项卡名称的tablayout保留了一个工具栏,我想更改这些选项卡名称中的字体,但我无法确定,因为名称不是实际的文本视图,我在论坛和其他任何地方都搜索过,但找不到任何人回答,所以简单地说,我想要的是改变这些标签中的字体名称这里是我的代码谢谢你的帮助 public class MainActivity extends AppCompatActivity { public Toolbar toolbar; private TabL

大家好,我正在开发一个选项卡式应用程序,其中我为应用程序徽标和带有选项卡名称的tablayout保留了一个工具栏,我想更改这些选项卡名称中的字体,但我无法确定,因为名称不是实际的文本视图,我在论坛和其他任何地方都搜索过,但找不到任何人回答,所以简单地说,我想要的是改变这些标签中的字体名称这里是我的代码谢谢你的帮助

public class MainActivity extends AppCompatActivity  {

public Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    viewPager.setCurrentItem(4);


}


private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    //here i set the fragments to use i have separate fragment.java
    // for each page i show that refers to another xml that shows the content
    //the words between "" are the tabs name and i need to change their fonts :(
    adapter.addFragment(new FiveFragment(),"ثقافة");
    adapter.addFragment(new FourFragment(), "فن");
    adapter.addFragment(new ThreeFragment(), "تعليم");
    adapter.addFragment(new TwoFragment(), "تاريخ");
    adapter.addFragment(new OneFragment(), "علوم");

    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

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

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
public类MainActivity扩展了AppCompatActivity{
公共工具栏;
私人摊位摊位;
私人视页机视页机;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(toolbar)findviewbyd(R.id.toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager=(viewPager)findViewById(R.id.viewPager);
设置viewPager(viewPager);
tabLayout=(tabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(4);
}
专用无效设置ViewPager(ViewPager ViewPager){
ViewPagerAdapter适配器=新的ViewPagerAdapter(getSupportFragmentManager());
//这里我设置了要使用的片段,我有单独的fragment.java
//对于我显示的每个页面,它引用了另一个显示内容的xml
//“”之间的文字是选项卡名称,我需要更改其字体:(
adapter.addFragment(新的FiveFragment(),“ثقافة”);
addFragment(新的FourFragment(),“فن”);
adapter.addFragment(新的ThreeFragment(),“تعليم”);
addFragment(新的TwoFragment(),“adapter.addFragment”);
addFragment(新的OneFragment(),“علوم”);
viewPager.setAdapter(适配器);
}
类ViewPagerAdapter扩展了FragmentPagerAdapter{
私有最终列表MFFragmentList=new ArrayList();
私有最终列表MFFragmentTitleList=new ArrayList();
公共视图页面编辑器(碎片管理器){
高级经理;
}
@凌驾
公共片段getItem(int位置){
返回MFFragmentList.get(位置);
}
@凌驾
public int getCount(){
返回MFFragmentList.size();
}
public void addFragment(片段片段,字符串标题){
添加(片段);
MFFragmentTitleList.add(标题);
}
@凌驾
公共字符序列getPageTitle(int位置){
返回MFFragmentTitleList.get(位置);
}
}
}

下面是xml的主要活动

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
            android:id="@+id/logo"
            android:src="@drawable/icon"
            android:layout_width="60dp"
            android:layout_height="58dp"
            android:layout_marginRight="323dp"
            android:layout_marginEnd="330dp"
            android:adjustViewBounds="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabTextColor="#000"
        app:tabTextAppearance="@style/MineCustomTabText"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#0ff"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


您可以为表格布局设置自定义样式以更改字体

使用样式 在
styles.xml中创建新样式:

<style name="AppTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabTextAppearance">@style/YourTextAppareance</item>
</style>
最后,将样式设置为您的表格布局:

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        style="@style/AppTabLayout"
        app:tabTextColor="#000"
        app:tabTextAppearance="@style/MineCustomTabText"
        app:tabGravity="fill"/>
然后,只需浏览选项卡列表并添加自定义视图

for (int i=1; i < mSectionsPagerAdapter.getCount(); i++)
{
    tabLayout.getTabAt(i).setCustomView(mSectionsPagerAdapter.getCustomView(i, null, false));
}

TabLayout有一个getTabAt方法,而那有一个setText方法,不是吗?我的问题实际上与字体更改有关。我想为我的标签名称设置一个自定义字体,先生^^"我看到了可能的重复。你还没有显示你的styles.xml,但我认为这篇文章就是你要找的。我不想改变单词的大小,我想更改字体,请再次阅读我的问题。请参考如何使用自定义字体?默认情况下样式不可用。如果你只支持API7+,这里是一个至少我认为这个库已经坏了,我已经试了一个小时了,每次使用它应用程序都会崩溃:/很多库都在样式上实现自定义字体,只需在github上检查一下就可以了。如果是,我编辑了我的帖子,以提供一个替代方案(但不是最佳方案)没有任何附加库的情况下,这种方式应该可以工作。我很好地应用了书法库,但它适用于工具栏和导航抽屉的碎片七,但不适用于选项卡名称!:(,感谢您迄今为止的帮助^ ^”
public class SectionsPageAdapter  extends FragmentPageViewAdapter {

public SectionsPageAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public View getCustomView(int position, View v, boolean selected) {

    if (v == null) {
        LayoutInflater li = LayoutInflater.from(App.getApplication());
        v = li.inflate(R.layout.view_tab, null);
    }

    TextView textView = (TextView) v.findViewById(R.id.textView);
    // Change your font on the textview here.

    textView.setText(getPageTitle(position));

    // Take in account the selected var if you want another layout when
    // tab is selected.

    return v;
}

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    if (position == 0)
        return Fragment.newInstance();
    else if (position == 1)
        return Fragment.newInstance();
    else
        return Fragment.newInstance();
}

@Override
public int getCount() {
    // Show 3 total pages.

    return 3;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "Tab 1";
        case 1:
            return "Tab 2";
        case 2:
            return "Tab 3";
    }
    return null;
}
}
for (int i=1; i < mSectionsPagerAdapter.getCount(); i++)
{
    tabLayout.getTabAt(i).setCustomView(mSectionsPagerAdapter.getCustomView(i, null, false));
}
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                if (i == position)
                    tabLayout.getTabAt(i).setCustomView(mSectionsPagerAdapter.getCustomView(i, tabLayout.getTabAt(i).getCustomView(), true));
                else
                    tabLayout.getTabAt(i).setCustomView(mSectionsPagerAdapter.getCustomView(i, tabLayout.getTabAt(i).getCustomView(), false));
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/DejaVuSerif-Italic.ttf");
newfont.setTypeface(font);