【问题标题】:android font size of tabs标签的android字体大小
【发布时间】:2016-03-14 09:18:36
【问题描述】:

我前几次问过这个任务,但我没有得到任何解决方案:( 我的问题是,我有一个带有标签活动的 android 应用程序,我必须在其中设置标签的字体大小,但我不知道如何。

在我的活动中,我以编程方式设置标签:

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("MY TAB 1"));
    tabLayout.addTab(tabLayout.newTab().setText("MY TAB 2"));
    tabLayout.addTab(tabLayout.newTab().setText("MY TAB 3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

问题是,最后 1 - 2 个字母会被剪掉,所以我必须将字体大小设置得更小,但是如何设置呢?

我希望任何人都可以帮助我。

【问题讨论】:

    标签: android tabs


    【解决方案1】:

    我做了以下事情:

    像这样创建 attrs.xml 条目

    <resources>
      <declare-styleable name="SegmentedTabLayout">
        <attr name="fontSize" format="dimension" />
      </declare-styleable>
    </resources>
    

    然后创建一个字体大小属性和初始化方法,您将像这样在构造函数中调用它

    public float FontSize { get; set; }
    
    private void InitializeAttrs(Context context, IAttributeSet attrs)
    {
        if (context == null || attrs == null)
                return;
    
        var styleAttributes = context.ObtainStyledAttributes(attrs, Resource.Styleable.SegmentedTabLayout);
    
        if (styleAttributes == null)
            return;
    
        FontSize = styleAttributes.GetDimension(Resource.Styleable.SegmentedTabLayout_fontSize, -1);
    
        styleAttributes.Recycle();
    }
    

    然后我用了virtual 9966的方法(稍微改进了一下),在override NewTab方法中这样调用

    public override Tab NewTab()
    {
        var tab = base.NewTab();
    
        ChangeFontSize(tab);
    
        return tab;
    }
    
    private void ChangeFontSize(Tab tab)
    {
        if (FontSize < 0)
            return;
    
        var tabViewGroup = (ViewGroup)tab.View;
        for (int i = 0; i < tabViewGroup.ChildCount; i++)
        {
            if (tabViewGroup.GetChildAt(i) is TextView textView)
            {
                textView.TextSize = FontSize;
            }
        }
    }
    

    就是这样:)

    【讨论】:

      【解决方案2】:

      如果你直接在选项卡布局样式中设置 text_size, 它不起作用! 您必须将其设置为具有 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"/>
      

      这也是选项卡的完整样式:

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

      【讨论】:

        【解决方案3】:

        如果您想以编程方式更改字体大小,可以使用 java 反射访问 TabLayout 类中的整数字段 tabTextSize 并根据您的设置设置字体大小要求。

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

        【讨论】:

          【解决方案4】:

          也可以试试:

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

          【讨论】:

            【解决方案5】:

            试试这个

            创建一个名为 custom_tab.xml 的 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"/>
            

            比在您的活动中设置文本大小编程方式如下代码

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

            【讨论】:

              【解决方案6】:
              #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);
              
                              }
                          }
                      }
                  }
              

              此代码适用于 Tablayout 更改文本颜色、类型面(字体样式)以及文本大小。如果这对您有用,那么也可以纠正,以便其他用户可以轻松解决他/她的问题

              【讨论】:

                【解决方案7】:

                在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>
                

                在你的tablayout中,设置如下样式。

                <android.support.design.widget.TabLayout
                     style="@style/MyTabLayout"
                     android:layout_width="width"
                     android:layout_height="height"/>
                

                【讨论】:

                • 您不应在字体中使用dp。使用sp
                • 如果此解决方案不适合您并且您的标签文本是多行的;在这里查看我的答案以获得解决方案:stackoverflow.com/a/48797329/700693
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-05-17
                • 2016-11-11
                • 1970-01-01
                • 2017-09-25
                • 2019-01-15
                • 2021-10-04
                相关资源
                最近更新 更多