android font size of tabs

后端 未结 7 1453
滥情空心
滥情空心 2020-11-29 13:25

i ask this quest for a few time ago , but i get no solutions :( my problem is, that i have an android app with a tab activity, where i have to set the font size of my tabs,

相关标签:
7条回答
  • 2020-11-29 13:33

    Write these below codes in styles.xml

    <style name="MyTabLayout" parent="Base.Widget.Design.TabLayout">
            <item name="tabTextAppearance">@style/MyTabTextAppearance</item>
    </style>
    
    <style name="MyTabTextAppearance" parent="TextAppearance.AppCompat.Button">
            <item name="android:textSize">18sp</item>
            <item name="android:textColor">@android:color/white</item>
            <item name="textAllCaps">true</item>
    </style>
    

    And in your tablayout, set the style like below.

    <android.support.design.widget.TabLayout
         style="@style/MyTabLayout"
         android:layout_width="width"
         android:layout_height="height"/>
    
    0 讨论(0)
  • 2020-11-29 13:33

    try this

    Create an xml layout named 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"
    android:textColor="@color/colorAccent"/>
    

    than in your activity set text size programaticlly like below code

    TextView tabOne = (TextView) 
    LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabOne.setText("ONE");
    tabOne.setTextSize(14); // set font size as per your requirement 
    tabLayout.getTabAt(0).setCustomView(tabOne);
    
    0 讨论(0)
  • 2020-11-29 13:33
    #If you dont want to use style.xml and do by prgramatically the Use this in your xml layout. #
    <android.support.design.widget.TabLayout
                    android:id="@+id/tab_Layout_dashboard"
                    android:layout_below="@id/ll_profile"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.15"
                    style="@style/Base.Widget.Design.TabLayout"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    />
    
                <android.support.v4.view.ViewPager
                    android:layout_below="@id/tab_Layout_dashboard"
                    android:id="@+id/pager_tutor"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.85"
                    />
    
    
    #In your activity or fragment use this method#
    
     private void changeTabsFont() {
            Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/"+ Constants.FontStyle);
            ViewGroup vg = (ViewGroup) tab_layout.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);
                        ((TextView) tabViewChild).setTextSize(15);
    
                    }
                }
            }
        }
    

    This code works for Tablayout change text color,type face(Font style) and also Text size. IF this works for you also then make correct so other user can easily solve his/her problem

    0 讨论(0)
  • 2020-11-29 13:38

    also try:

    <style name="MyCustomTabText" parent="TextAppearance.Design.Tab">
       <item name="android:textSize">14sp</item>
        <item name="android:textColor">?android:textColorSecondary</item>
        <item name="textAllCaps">false</item>
    </style>
    
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabTextAppearance="@style/MyCustomTabText" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    0 讨论(0)
  • 2020-11-29 13:42

    if you set text_size directly in style of tab layout, it doesn't work!! you have to set it into a style which has parent="@android:style/TextAppearance.Widget.TabWidget"

    <style name="tab_text" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="android:fontFamily">@fonts/iransans_b</item>
        <item name="android:textSize">@dimen/textSize_Large</item>
        <item name="textAllCaps">false</item>
    </style>
    
    <style name="Tab" parent="Widget.Design.TabLayout">
        <item name="tabTextAppearance">@style/tab_text</item>
    </style>
    
    
     <com.google.android.material.tabs.TabLayout
                     android:id="@+id/item_tabs"
                     android:layout_width="match_parent"
                     android:layout_height="@dimen/margin_dp_65"
                     style="@style/Tab"/>
    

    and also this is a full style for tab:

     <style name="Tab_WithBackground" parent="Widget.Design.TabLayout">
        <item name="tabSelectedTextColor">@color/purple</item>
        <item name="tabTextColor">@drawable/tab_text_color</item>
        <item name="tabIndicatorColor">@color/white</item>
        <item name="tabGravity">fill</item>
        <item name="tabIndicatorHeight">4dp</item>
        <item name="android:tabStripEnabled">true</item>
        <item name="android:padding">0dp</item>
        <item name="tabMaxWidth">0dp</item>
        <item name="android:minHeight">@dimen/margin_dp_80</item>
        <item name="tabTextAppearance">@style/tab_text</item>
    </style>
    
    0 讨论(0)
  • 2020-11-29 13:44

    If you want to change the font size programmatically, you can use java reflection to access the integer field tabTextSize in the TabLayout class and set the font size as per your requirement.

    public static void updateTabLayoutFontSize(TabLayout tabLayout, int textSizeInPixel) {
      try {
         Field mCursorDrawableRes = TabLayout.class.getDeclaredField("tabTextSize");
         mCursorDrawableRes.setAccessible(true);
         mCursorDrawableRes.set(tabLayout, textSizeInPixel);
      } catch (Exception e) {
        Log.d("TAG1", "Failed to update tablayout font using reflection");
      }
    }
    
    0 讨论(0)
提交回复
热议问题