我正在寻找具有以下自定义行为的TabLayout
我附上了以下代码片段,我尝试过它。
1)我创建了自定义视图,名为FundayTabLayout
<com.knackv.custom.view.FundayTabLayout
android:id="@id/dashboard_tab_layout"
style="@style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="@style/AppTabTextAppearance"></com.knackv.custom.view.FundayTabLayout>
2)我使用的样式
<style name="AppTabLayout" parent="Widget.Design.TabLayout">
<item name="tabMaxWidth">@dimen/size_in_150_dp</item>
<item name="tabIndicatorColor">@color/app_background</item>
<item name="tabBackground">@drawable/tab_background_color_selector</item>
<item name="tabTextAppearance">@style/AppTabTextAppearance</item>
<item name="tabSelectedTextColor">@color/app_blue</item>
</style>
<style name="AppTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">@dimen/size_in_15_dp</item>
<item name="textAllCaps">false</item>
</style>
3)设置我的自定义字体:
@Override
public void setupWithViewPager(@Nullable ViewPager viewPager) {
super.setupWithViewPager(viewPager);
this.removeAllTabs();
ViewGroup slidingTabStrip = (ViewGroup) getChildAt(0);
for (int i = 0, count = viewPager.getAdapter().getCount(); i < count; i++) {
Tab tab = this.newTab();
this.addTab(tab.setText(viewPager.getAdapter().getPageTitle(i)));
AppCompatTextView view = (AppCompatTextView) ((ViewGroup) slidingTabStrip.getChildAt(i)).getChildAt(1);
view.setAllCaps(false);
view.setTypeface(Utility.getNormalTypeFace());
}
}
请为此问题提供最佳解决方案。
答案 0 :(得分:0)
Typeface fontTypeFace = Typeface.createFromAsset(getAssets(), "OpenSans-Semibold.ttf");
for (int i = 0; i < tabLayout.getChildCount(); ++i) {
View nextChild = tabLayout.getChildAt(i);
if (nextChild instanceof TextView) {
TextView textViewToConvert = (TextView) nextChild;
textViewToConvert.setTypeface(fontTypeFace);
}
}
试试这个
答案 1 :(得分:0)
https://stackoverflow.com/a/31067431/5726971 检查一下..它主要用于Android设计支持TabLayout。 https://stackoverflow.com/a/33284990/5726971 如果像你的情况一样使用自定义TabLayout,这个就添加了第一个答案。