android font size of tabs

后端 未结 7 1454
滥情空心
滥情空心 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:48

    I've done the following:

    Create a attrs.xml entry like this

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

    Then create a font size property and initialise method that you'll call in the constructor like this

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

    Then I used the method of viral 9966 (improved it a little) and called it in the override NewTab method like this

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

    And that's it :)

    0 讨论(0)
提交回复
热议问题