如何更改选项卡视图中的字体?

时间:2018-01-15 06:44:35

标签: android fonts

我正在使用片段中的标签视图。
我想在使用书法时更改字体这里是链接:Calligraphy
除了标签视图,我可以更改整个应用程序的字体。
班级代码在这里,如果你能解决问题,我真的很感激,我被困在这里。
这是我的代码:

   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);
    }
}

3 个答案:

答案 0 :(得分:3)

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>

自定义字体

 <item name="android:fontFamily">fontname</item>

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" />

此属性用于更改 TabLayout

中的文字样式
  

应用程式:tabTextAppearance = “@风格/ CustomViewAllTab”

答案 1 :(得分:1)

这样做 - :

private void changeTabsFont() {

        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(Font.getInstance().getTypeFace(), Typeface.NORMAL);
                }
            }
        }
    } 

答案 2 :(得分:0)

尝试使用样式

<style name="TabWidget" parent="TextAppearance.AppCompat.Medium">
        <item name="android:textSize">16sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">monospace</item>
</style>

xml中的tablayout

中使用此功能
app:tabTextAppearance="@style/TabWidget"

示例

<android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorBlack"
        app:tabTextAppearance="@style/TabWidget"
        app:tabTextColor="@android:color/darker_gray" />