2016-12-11 82 views
0

我要更改TabLayout自定义字体,但我不知道为什么它不会再更改,我有一个4选项卡和一个ViewPager并有4 fragment。现在问题是我想更改标签文本字体。事实上,我用书法的不同部分,但我不知道为什么我不能将其设置为我自己的选项卡。我现在使用的东西是我开发了FragmentPagerAdapter's 方法并在那里设置自定义选项卡并在其中加载自定义选项卡布局xml文件。tablayout自定义字体不会更改

这是我的代码:

package com.example.noavaran.drugstore.Activity; 

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.PorterDuff; 
import android.graphics.Typeface; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.AppCompatTextView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.example.noavaran.drugstore.Fragment.AddressFragment; 
import com.example.noavaran.drugstore.Fragment.IssueFragment; 
import com.example.noavaran.drugstore.Fragment.MenuFragment; 
import com.example.noavaran.drugstore.Fragment.SearchFragment; 
import com.example.noavaran.drugstore.Helper.CustomViewPager; 
import com.example.noavaran.drugstore.R; 

import java.util.ArrayList; 
import java.util.List; 

import layout.fragment_approve; 
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; 
import uk.co.chrisjenx.calligraphy.CalligraphyUtils; 

public class MainPageActivity extends AppCompatActivity { 
    private TabLayout tabLayout; 
    public Context context; 
    public Typeface tf; 
    public static boolean a=false; 
    private CustomViewPager viewPager; 
    private int[] tabIcons = { 
      R.drawable.menu, 
      R.drawable.address_icon, 
      R.drawable.search_icon, 
      R.drawable.issue_icon 
    }; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_page); 
     tf = Typeface.createFromAsset(getAssets(), "IRANSans_Bold.ttf"); 


     tabCustomization(); 
     changeTabsFont(); 
     setupTabIcons(); 
    } 

    private void tabCustomization() { 
     viewPager = (CustomViewPager) findViewById(R.id.viewPager); 
     viewPager.setPagingEnabled(false); 

     setupViewPager(viewPager); 
     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 
// Iterate over all tabs and set the custom view 

    } 
    private void setupTabIcons() { 

     tabLayout.getTabAt(0).setIcon(tabIcons[0]); 
     tabLayout.getTabAt(1).setIcon(tabIcons[1]); 
     tabLayout.getTabAt(2).setIcon(tabIcons[2]); 
     tabLayout.getTabAt(3).setIcon(tabIcons[3]); 


     tabLayout.getTabAt(0).getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN); 
     tabLayout.getTabAt(1).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN); 
     tabLayout.getTabAt(2).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN); 
     tabLayout.getTabAt(3).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN); 

     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){ 

      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       tab.getIcon().setColorFilter(Color.parseColor("#a8a8a8"), PorterDuff.Mode.SRC_IN); 

      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 
       tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN); 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 
    } 
    public void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 


      adapter.addFragment(new MenuFragment(), "menu"); 

     adapter.addFragment(new AddressFragment(), "Address"); 
     adapter.addFragment(new SearchFragment(), "Search"); 
     adapter.addFragment(new IssueFragment(), "Issue"); 
     for (int i = 0; i < tabLayout.getTabCount(); i++) { 
      TabLayout.Tab tab = tabLayout.getTabAt(i); 

      tab.setCustomView(adapter.getTabView(i)); 
     } 
     viewPager.setAdapter(adapter); 

    } 

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 


     @Override 
     public Fragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 
     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFragment(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 
     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.imageView); 
      imageView .setImageResource(tabIcons[position]); 

      return view; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 

    } 
    private void changeTabsFont() { 
     Typeface mTypeface = Typeface.createFromAsset(getAssets(), "IRANSans_Bold.ttf"); 
     ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0); 
     int tabsCount = vg.getChildCount(); 
     for (int j = 0; j < tabsCount; j++) { 
      ViewGroup vgTab = (ViewGroup) vg.getChildAt(j); 
      int tabChildsCount = vgTab.getChildCount(); 
      for (int i = 0; i < tabChildsCount; i++) { 
       View tabViewChild = vgTab.getChildAt(i); 
       if (tabViewChild instanceof TextView) { 
        ((TextView) tabViewChild).setTypeface(mTypeface, Typeface.BOLD); 
       } 
      } 
     } 
    } 
    //Caligraphy Library 
    @Override 
    protected void attachBaseContext(Context newBase) { 
     super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
    } 
} 

,这是我customxml文件:

<?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:textSize="@dimen/text_size" 
     android:gravity="center" 
     fontPath="@string/fontsIran" 
     android:id="@+id/textView" /> 
</LinearLayout> 
+0

你有自定义字体'tf',但你在哪里设置它的标签? –

+0

我没有看到你在'getTabView()'中初​​始化你正在从'LayoutInflater'获得'context'字段的位置。 –

+0

@MikeM。你是什​​么意思关于上下文领域的布局inflater – Sandro

回答

0

使用这个库。

添加到您的build.gradle文件

// fonts 
compile 'in.workarounds.typography:typography:0.0.8' 

然后更改TextView到,

<in.workarounds.typography.TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="New Text" 
    android:textSize="@dimen/text_size" 
    android:gravity="center" 
    fontPath="@string/fontsIran" 
    android:id="@+id/textView" 
    app:font_name="Lato" 
    app:font_variant="Light"/> 

其中字体为Lato-Light。其中位于assets/fonts/Lato-Light.ttf