vin*_*yas 5 android android-tablelayout
如何使用属于android.support.design.widget包的Tablayout类的自定义字体?我正在使用它来实现快速返回视图功能.
小智 27
从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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
所有功劳都归功于Andreyua的原始代码片段,并进行了少量修改.
不幸的是,我没有足够的声誉来发表评论,或者我会直接回复:)
ilw*_*ilw 18
试试这个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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
使用 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11459 次 |
| 最近记录: |