如何在标签视图中更改字体?

kal*_*ali 2 fonts android

我在一个片段中使用选项卡视图。
我想在使用书法时更改字体,这是链接:书法
除了选项卡视图,我可以在整个应用程序中更改字体。
该类的代码在这里,如果您能解决问题,
我真的很感激,我被困在这里。
这是我的代码:

   public class AsliFragment extends Fragment {


    @SuppressLint("StaticFieldLeak")
    public static ViewPager viewPager;
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    public AsliFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            getArguments().getString(ARG_PARAM1);
            getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_asli, container, false);

        //init tablayout & viewPager
        initViewPager(view);

    return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
        } else {
           // Toast.makeText(context,"Remove Fregment Attached", Toast.LENGTH_SHORT).show();
        }
    }



    @Override
    public void onDetach() {
        super.onDetach();
    }


    interface OnFragmentInteractionListener {
    }

    private void initViewPager(View view){
        viewPager = view.findViewById(R.id.viewPager);
        TabLayout tabLayout = view.findViewById(R.id.tabLayout);
        //create adapter


        adabterViewPager adapter = new adabterViewPager(getChildFragmentManager());
        //get string tab name
        String tab_name_1 = getResources().getString(R.string.tab_name_1);
        String tab_name_2 = getResources().getString(R.string.tab_name_2);
        //add fragment to adapter
        adapter.addFragment(new FragmentOneAsli(), tab_name_1);
        adapter.addFragment(new FragmentTwoAsli(), tab_name_2);
        //set adapter to viewpager
        viewPager.setAdapter(adapter);
        //set tablayout with viewpager
        tabLayout.setupWithViewPager(viewPager);
    }
}
Run Code Online (Sandbox Code Playgroud)

Lok*_*sai 5

style.xml中添加

 <style name="CustomViewAllTab" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="android:textSize">20sp</item>
        <item name="android:fontFamily">@string/fontHelveticaMed</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

为您的自定义字体

 <item name="android:fontFamily">fontname</item>
Run Code Online (Sandbox Code Playgroud)

TabLayout上应用样式

 <android.support.design.widget.TabLayout
        android:id="@+id/tabOrderType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorViewAllTab"
        app:tabBackground="@color/colorViewAllTab"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMaxWidth="0dp"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorSelectedText"
        app:tabTextAppearance="@style/CustomViewAllTab"
        app:tabTextColor="@color/colorUnselectedTabColor" />
Run Code Online (Sandbox Code Playgroud)

此属性用于更改TabLayout中的文本样式

app:tabTextAppearance =“ @ style / CustomViewAllTab”