我正在尝试开发android设计库中的新TabLayout
。
我想将制表符文本更改为自定义字体。而且,我试图搜索一些与TabLayout
相关的样式,但最终搜索到了this。
请指导我如何更改制表符文本字体。
发布于 2017-07-31 14:57:27
从Java Code或XML创建一个TextView,如下所示
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:textSize="15sp"
android:textColor="@color/tabs_default_color"
android:gravity="center"
android:layout_height="match_parent"
/>
确保id在这里保持不变,因为如果您使用自定义文本视图,TabLayout将检查此ID
然后,从代码中膨胀此布局,在该文本视图上设置自定义Typeface
,并将此自定义视图添加到选项卡
for (int i = 0; i < tabLayout.getTabCount(); i++) {
//noinspection ConstantConditions
TextView tv = (TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
tv.setTypeface(Typeface);
tabLayout.getTabAt(i).setCustomView(tv);
}
发布于 2015-06-26 15:28:11
如果您正在使用TabLayout
,并且您想要更改字体,则必须在以前的解决方案中添加一个新的for循环,如下所示:
private void changeTabsFont() {
ViewGroup vg = (ViewGroup) tabLayout.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.getInstance().getTypeFace(), Typeface.NORMAL);
}
}
}
}
发布于 2017-08-22 19:51:08
创建您自己的自定义样式并将父样式用作parent="@android:style/TextAppearance.Widget.TabWidget"
并在选项卡布局中使用此样式作为app:tabTextAppearance="@style/tab_text"
示例: Style:
<style name="tab_text" parent="@android:style/TextAppearance.Widget.TabWidget">
<item name="android:fontFamily">@font/poppins_regular</item>
</style>
示例:选项卡布局组件:
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabTextAppearance="@style/tab_text" />
https://stackoverflow.com/questions/31067265
复制相似问题