如何更改 TabLayout 中的字体大小?
How to change font size in TabLayout?
在我的应用程序中,我想使用 TabLayout
并且我使用了这个库:
https://github.com/LiushuiXiaoxia/TabLayoutPlus
但是,文字太大了,我想改一下。为此,我在 style.xml
中设置了自定义样式,但它不会更改文本大小。
XML代码:
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
android:id="@+id/fullSearch_tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fullSearch_toolbar"
android:background="@color/colorPrimary"
android:paddingBottom="@dimen/size2"
android:paddingLeft="@dimen/size2"
android:paddingRight="@dimen/size2"
app:tabIndicatorColor="@color/whiteMe"
app:tabSelectedTextColor="@color/whiteMe"
app:tabTextAppearance="@style/allCapsTabLayout_search"
app:tabTextColor="@color/unSelectTab" />
<style name="allCapsTabLayout_search" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">@dimen/font5</item>
</style>
如何更改文字大小?
转到
1> Your Library Module
2> open a file "tablayoutplus_custom_view.xml"
3> set android:textSize="20sp" into "android:id="@+id/tvTabText""
或
在 库 模块中以编程方式设置 "BadgedTabCustomView.java":
tvTabText.setTextSize(20);
希望现在对您有所帮助。
使用这个:使用父样式作为
parent="Widget.Design.TabLayout"
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<!--<item name="tabMaxWidth">@dimen/tab_max_width</item>-->
<item name="tabIndicatorHeight">2dp</item>
<item name="tabPaddingStart">10dp</item>
<item name="tabPaddingEnd">10dp</item>
<item name="tabBackground">@color/grey_200</item>
<item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">@color/appred</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">20sp</item>
<item name="android:textColor">@color/grey_800</item>
<item name="textAllCaps">true</item>
</style>
然后在 Tablayout 中应用您的样式:
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
//
style="@style/MyCustomTabLayout"
// />
(或)
以编程方式使用此更改
for (int i = 0; i < tabLayout.getTabCount(); i++) {
BadgedTabCustomView customView = tabLayout.getTabCustomViewAt(i);
if (customView != null) {
customView.setTabText("Tab" + (i + 1));
customView.setTabCount(i);
customView.setTextSize(20);
}
}
@RedBounce 希望下面的方法对你有用。 style
属性可能在您的 TabLayout
中不起作用,因为您使用了自定义 tabview。
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
打开 TabLayoutPlus.java
文件并在 class
中找到下面的方法
private BadgedTabCustomView initTab(TabLayout.Tab tab) {
BadgedTabCustomView customView = new BadgedTabCustomView(getContext());
customView.tvTabText.setTextColor(getTabTextColors());
//add your tab font size here as you want
customView.tvTabText.setTextSize(20);
customView.tvTabSubText.setTextColor(getTabTextColors());
if (subTextSize > 0) {
customView.tvTabSubText.setTextSize(TypedValue.COMPLEX_UNIT_PX, subTextSize);
}
customView.tvTabCount.setTextColor(countTextColor);
if (countTextSize > 0) {
customView.tvTabCount.setTextSize(TypedValue.COMPLEX_UNIT_PX, countTextSize);
}
customView.tvTabCount.setBackgroundDrawable(countTextBackground);
customView.setTabText(tab.getText());
tab.setCustomView(customView);
return customView;
}
在这个方法的下面一行
customView.tvTabText.setTextSize(20);
//20是字体大小
在我的应用程序中,我想使用 TabLayout
并且我使用了这个库:
https://github.com/LiushuiXiaoxia/TabLayoutPlus
但是,文字太大了,我想改一下。为此,我在 style.xml
中设置了自定义样式,但它不会更改文本大小。
XML代码:
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
android:id="@+id/fullSearch_tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fullSearch_toolbar"
android:background="@color/colorPrimary"
android:paddingBottom="@dimen/size2"
android:paddingLeft="@dimen/size2"
android:paddingRight="@dimen/size2"
app:tabIndicatorColor="@color/whiteMe"
app:tabSelectedTextColor="@color/whiteMe"
app:tabTextAppearance="@style/allCapsTabLayout_search"
app:tabTextColor="@color/unSelectTab" />
<style name="allCapsTabLayout_search" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">@dimen/font5</item>
</style>
如何更改文字大小?
转到
1> Your Library Module
2> open a file "tablayoutplus_custom_view.xml"
3> set android:textSize="20sp" into "android:id="@+id/tvTabText""
或
在 库 模块中以编程方式设置 "BadgedTabCustomView.java":
tvTabText.setTextSize(20);
希望现在对您有所帮助。
使用这个:使用父样式作为
parent="Widget.Design.TabLayout"
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<!--<item name="tabMaxWidth">@dimen/tab_max_width</item>-->
<item name="tabIndicatorHeight">2dp</item>
<item name="tabPaddingStart">10dp</item>
<item name="tabPaddingEnd">10dp</item>
<item name="tabBackground">@color/grey_200</item>
<item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">@color/appred</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">20sp</item>
<item name="android:textColor">@color/grey_800</item>
<item name="textAllCaps">true</item>
</style>
然后在 Tablayout 中应用您的样式:
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
//
style="@style/MyCustomTabLayout"
// />
(或) 以编程方式使用此更改
for (int i = 0; i < tabLayout.getTabCount(); i++) {
BadgedTabCustomView customView = tabLayout.getTabCustomViewAt(i);
if (customView != null) {
customView.setTabText("Tab" + (i + 1));
customView.setTabCount(i);
customView.setTextSize(20);
}
}
@RedBounce 希望下面的方法对你有用。 style
属性可能在您的 TabLayout
中不起作用,因为您使用了自定义 tabview。
<cn.mycommons.tablayoutplus.library.TabLayoutPlus
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
打开 TabLayoutPlus.java
文件并在 class
private BadgedTabCustomView initTab(TabLayout.Tab tab) {
BadgedTabCustomView customView = new BadgedTabCustomView(getContext());
customView.tvTabText.setTextColor(getTabTextColors());
//add your tab font size here as you want
customView.tvTabText.setTextSize(20);
customView.tvTabSubText.setTextColor(getTabTextColors());
if (subTextSize > 0) {
customView.tvTabSubText.setTextSize(TypedValue.COMPLEX_UNIT_PX, subTextSize);
}
customView.tvTabCount.setTextColor(countTextColor);
if (countTextSize > 0) {
customView.tvTabCount.setTextSize(TypedValue.COMPLEX_UNIT_PX, countTextSize);
}
customView.tvTabCount.setBackgroundDrawable(countTextBackground);
customView.setTabText(tab.getText());
tab.setCustomView(customView);
return customView;
}
在这个方法的下面一行
customView.tvTabText.setTextSize(20);
//20是字体大小