TabLayout 从代码中设置 TabLayout.Tab 的文本大小(以编程方式)
TabLayout set text size of TabLayout.Tab from code (programmatically)
我正在尝试通过代码设置文本大小,因为这个选项不存在,有人知道如何实现吗?
我知道可以通过样式实现,但我不能使用样式。
我也尝试了 示例,但它不起作用。
我已经部分(一些标签获得了新的文本大小)成功了:
try {
Field tabTextSize = TabLayout.class.getDeclaredField("mTabTextSize");
tabTextSize.setAccessible(true);
tabTextSize.setFloat(mTabLayout, 64f);
} catch (Exception e) {
e.printStackTrace();
}
试试这个
创建一个名为 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"/>
比在您的 activity 中设置文本大小更像下面的代码
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);
我正在尝试通过代码设置文本大小,因为这个选项不存在,有人知道如何实现吗?
我知道可以通过样式实现,但我不能使用样式。
我也尝试了
我已经部分(一些标签获得了新的文本大小)成功了:
try {
Field tabTextSize = TabLayout.class.getDeclaredField("mTabTextSize");
tabTextSize.setAccessible(true);
tabTextSize.setFloat(mTabLayout, 64f);
} catch (Exception e) {
e.printStackTrace();
}
试试这个
创建一个名为 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"/>
比在您的 activity 中设置文本大小更像下面的代码
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);