[英]how to custom font for android.support.design.widget.TabLayout using Calligraphy
[英]Custom font for android.support.design.widget.TabLayout
如何為屬於android.support.design.widget包的Tablayout類使用自定義字體? 我正在使用它來實現快速返回視圖功能。
從23.2.0開始,setTabsFromPagerAdapter已被棄用,但是使用Andreyua的答案的修改版本,您可以使用setupWithViewPager。
@Override
public void setupWithViewPager(ViewPager viewPager)
{
super.setupWithViewPager(viewPager);
if (mTypeface != null)
{
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);
PagerAdapter adapter = viewPager.getAdapter();
for (int i = 0, count = adapter.getCount(); i < count; i++)
{
Tab tab = this.newTab();
this.addTab(tab.setText(adapter.getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup) slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setTypeface(mTypeface, Typeface.NORMAL);
}
}
}
所有功勞都歸功於Andreyua的原始代碼片段,並進行了少量修改。
不幸的是,我沒有足夠的聲譽來發表評論,或者我會直接回復:)
試試這個CustomTabLayout
public class CustomTabLayout extends TabLayout {
public CustomTabLayout(Context context) {
super(context);
}
public CustomTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) {
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Medium.ttf");
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);
for (int i = 0, count = adapter.getCount(); i < count; i++) {
Tab tab = this.newTab();
this.addTab(tab.setText(adapter.getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup)slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setTypeface(typeface, Typeface.NORMAL);
}
}
}
使用android支持庫26.2.0,您可以指定樣式中的字體
<style name="TabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/TabText</item>
<item name="tabSelectedTextColor">@color/white</item>
<item name="tabIndicatorColor">@color/white</item>
</style>
<style name="TabText" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/lite</item>
<!--Here below-->
<item name="android:fontFamily">@font/gotham_medium</item>
</style>
Extension Function
Kotlin Version
要將自定義字體設置為TabLayout
嘗試在kotlin文件中添加以下代碼片段作為擴展函數(例如,我創建了一個名為Extentions.tk
的文件):
fun TabLayout.applyFont(typeface: Typeface) {
val viewGroup = getChildAt(0) as ViewGroup
val tabsCount = viewGroup.childCount
for (j in 0 until tabsCount) {
val viewGroupChildAt = viewGroup.getChildAt(j) as ViewGroup
val tabChildCount = viewGroupChildAt.childCount
for (i in 0 until tabChildCount) {
val tabViewChild = viewGroupChildAt.getChildAt(i)
if (tabViewChild is TextView) {
tabViewChild.typeface = typeface
}
}
}
}
。
Usage
現在您可以簡單地使用它如下:
val typeface = Typeface.createFromAsset(context.assets, "fonts/Roboto.ttf")
tabLayout.applyFont(typeface)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.