如何使用书法为 android.support.design.widget.TabLayout 自定义字体
how to custom font for android.support.design.widget.TabLayout using Calligraphy
我已经使用 Calligraphy 来自定义我的应用字体...
我的应用包含 android.support.design.widget.TabLayout...
我的应用程序的字体已更改并且它可以正常工作,除了 tabLayout 标题和 TabLayout 中的选项卡标题使用设备字体而不是自定义字体。
选项卡有文本和图标。我有 mFragmentTitleList 数组用于标题和 tabIcons 数组...
我尝试按照此处说明的方式使用样式和主题:calligraphy
但是 none 他们工作了...
答案是使用自定义视图...我为每个选项卡应用了自定义 XML 布局...为此,应该在附加滑动后遍历所有 TabLayout.Tabs寻呼机的选项卡:
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
MyFragmentPagerAdapter pagerAdapter =
new MyFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
接下来我们应该将 getTabView(position) 方法添加到 MyFragmentPagerAdapter class:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String tabTitles[] = new String[] { "Tab1", "Tab2","Tab3" };
private int[] tabIcons = {
R.drawable.ic_one,
R.drawable.ic_two,
R.drawable.ic_three
};
public View getTabView(int position) {
// Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
View view= LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
TextView textView= (TextView) view.findViewById(R.id.textView);
textView.setText(getPageTitle(position));
ImageView imageView = (ImageView) view.findViewById(R.id.imgView);
imageView .setImageResource(tabIcons[position]);
return view;
}
}
这是自定义视图的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:gravity="center"
android:textColor="@drawable/sel_tab_text"
android:id="@+id/textView" />
我已经使用 Calligraphy 来自定义我的应用字体... 我的应用包含 android.support.design.widget.TabLayout... 我的应用程序的字体已更改并且它可以正常工作,除了 tabLayout 标题和 TabLayout 中的选项卡标题使用设备字体而不是自定义字体。 选项卡有文本和图标。我有 mFragmentTitleList 数组用于标题和 tabIcons 数组... 我尝试按照此处说明的方式使用样式和主题:calligraphy 但是 none 他们工作了...
答案是使用自定义视图...我为每个选项卡应用了自定义 XML 布局...为此,应该在附加滑动后遍历所有 TabLayout.Tabs寻呼机的选项卡:
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
MyFragmentPagerAdapter pagerAdapter =
new MyFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
接下来我们应该将 getTabView(position) 方法添加到 MyFragmentPagerAdapter class:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String tabTitles[] = new String[] { "Tab1", "Tab2","Tab3" };
private int[] tabIcons = {
R.drawable.ic_one,
R.drawable.ic_two,
R.drawable.ic_three
};
public View getTabView(int position) {
// Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
View view= LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
TextView textView= (TextView) view.findViewById(R.id.textView);
textView.setText(getPageTitle(position));
ImageView imageView = (ImageView) view.findViewById(R.id.imgView);
imageView .setImageResource(tabIcons[position]);
return view;
}
}
这是自定义视图的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:gravity="center"
android:textColor="@drawable/sel_tab_text"
android:id="@+id/textView" />