簡體   English   中英

如何使用書法為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中的Tab標題使用設備字體外,都可以使用自定義字體。 標簽上有文字和圖標。 我有用於標題和tabIcons數組的mFragmentTitleList數組...我試圖以這里說明的方式使用樣式和主題: 書法,但是它們都不起作用...

答案是使用自定義視圖...我為每個選項卡應用了自定義XML布局...要實現此目的,應在將滑動式選項卡附加到尋呼機后遍歷所有TabLayout.Tab:

// 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類中:

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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM