TabLayout set text size of TabLayout.Tab from code (programmatically)

╄→гoц情女王★ 提交于 2020-12-13 04:17:49

问题


I'm trying to set up a text size from code, since this option does not exist does anyone have an idea how to achieve this?

I know it's possible through style, but I can't use style.

Also I tried this example, but it doesn't work.

I have partially (some Tabs get new text size) succes with this:

try {
        Field tabTextSize = TabLayout.class.getDeclaredField("mTabTextSize");
        tabTextSize.setAccessible(true);
        tabTextSize.setFloat(mTabLayout, 64f);
    } catch (Exception e) {
        e.printStackTrace();
    }

回答1:


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


来源:https://stackoverflow.com/questions/46972544/tablayout-set-text-size-of-tablayout-tab-from-code-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!