如何更改 google 支持在 android 中使用自定义字体设计滑动标签样式

How to change google support design sliding tab style with custom fonts in android

我正在通过 google 支持设计制作带有滑动标签的 android 应用程序。我想在滑动标签名称中设置自定义字体。现在我可以更改选项卡名称的样式和大小,但无法更改。

这里我想更改 SAMA 体育场和 Manjalpur 体育场的字体。 我已经尝试过了,但我认为我们不能从中更改字体样式。 那么,我该如何更改这种字体样式。 我在资产文件夹中的自定义字体是 HelveticaLTStd-UltraComp.otf

<style name="TextStyle" parent="TextAppearance.Design.Tab" >
        <item name="android:textSize">15dp</item>
        <item name="android:textStyle">bold</item>
    </style>

我的代码

<android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView2"
            android:background="@drawable/custom_tabback"
            app:tabIndicatorColor="#ec3c08"
            app:tabIndicatorHeight="6dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="#ec3c08"
            app:tabTextColor="#22b14c"
            app:tabTextAppearance="@style/TextStyle"/>

     

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tab"
            android:background="#FFFFFF"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_collapseMode="pin"
            app:tabMode="scrollable" />

public class Home extends AppCompatActivity{

Toolbar toolbar;
private ViewPager mPager;
private TabAdapter mAdapter;
private TabLayout mTabLayout;
DatabaseOperation databaseOperation;
boolean doubleBackToExitPressedOnce;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerlayout), toolbar);

    databaseOperation = new DatabaseOperation(Home.this);

    sharedPreferences = getSharedPreferences("pos", Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();

    String pos = sharedPreferences.getString("position", "");

    mTabLayout = (TabLayout) findViewById(R.id.tab);

    mPager = (ViewPager) findViewById(R.id.view_pager);

    mAdapter = new TabAdapter(getSupportFragmentManager());

    mPager.setAdapter(mAdapter);

    mTabLayout.setTabsFromPagerAdapter(mAdapter);

    mTabLayout.setupWithViewPager(mPager);

    mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

    setTitle("");

    if (!(pos.equals(""))) {
        Log.d("pos", pos);
        mPager.setCurrentItem(Integer.valueOf(pos) - 1);
    }


}

@Override
protected void onPause() {
    super.onPause();

}

class TabAdapter extends FragmentStatePagerAdapter {

    String[] tabsName = {"Sama stadium", "Manjalpur Stadium"};

    public TabAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            SamaStadium samaStadium = new SamaStadium();
            return samaStadium;
        } else {
            MakarpuraStadium makarpuraStadium = new MakarpuraStadium();
            return makarpuraStadium;
        }
    }

    @Override
    public int getCount() {
        return tabsName.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabsName[position];
    }
}

}

为自定义字体使用书法

calligraphy

申请中class,

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build();

然后每个视图的自定义字体为

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Roboto-Bold.ttf"/>

还有更多请关注link。

或将 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);
    }
}

}

要在整个应用程序中应用自定义字体,请使用此 link:https://gist.github.com/artem-zinnatullin/7749076

CustomTabLayout.java

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.ViewGroup;

/**
 * Created by HP-HP
 */
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/avenir_next_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);
        }
    }
}