【问题标题】:How can I change the font on my tab view? [duplicate]如何更改标签视图上的字体? [复制]
【发布时间】:2018-06-23 19:04:37
【问题描述】:

我在片段中使用选项卡视图。
我想在使用书法时更改字体这里是链接: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);
    }
}

【问题讨论】:

    标签: android fonts


    【解决方案1】:

    在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

    中的文本样式

    app:tabTextAppearance="@style/CustomViewAllTab"

    【讨论】:

    • 我在 assets 中有一个文件夹,还有一个名为 fonts 的文件夹,在该字体中名为 bm.ttf,如何在字体系列中使用它?
    • @kali FontName.ttf 试试这个..
    • 它不起作用:(
    • 给出你添加字体的完整路径
    • \src\main\assets\fonts\bm.ttf
    【解决方案2】:

    这样做-:

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

    【讨论】:

    • 感谢 Shivam 的回答。我应该如何根据我的课程个性化此代码?
    【解决方案3】:

    试试这个风格

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

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2021-09-11
      • 2019-09-10
      • 2021-09-20
      • 2011-06-23
      • 1970-01-01
      相关资源
      最近更新 更多