如何更改从PagerSlidingTabStrip创建的标签标题的字体?

时间:2014-04-24 15:18:11

标签: java android

以下是相关图书馆:https://github.com/astuetz/PagerSlidingTabStrip 我认为它与.setTypeface()有关,但我不知道如何使用它。我把我想要的字体放在assets文件夹中。这是我的代码。

Start.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;


public class Start extends FragmentActivity {

    private final Handler handler = new Handler();
    private PagerSlidingTabStrip tabs;
    private ViewPager pager;
    private MyPagerAdapter adapter;
    private int currentColor = 0xFF547CC1;

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

        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new MyPagerAdapter(getSupportFragmentManager());

        pager.setAdapter(adapter);
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                .getDisplayMetrics());
        pager.setPageMargin(pageMargin);
        tabs.setViewPager(pager);
        tabs.setIndicatorColor(currentColor);
        /*Typeface tf = Typeface.createFromAsset(getAssets(), "RobotoCondensed-Regular.ttf");
        tabs.setTypeface(tf,Typeface.NORMAL);*/

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }else if(id == R.id.add_location){
            Toast.makeText(getApplicationContext(), "todo", Toast.LENGTH_SHORT).show();
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Enter a location");
            alert.setMessage("Enter a zipcode or city");

            final EditText input = new EditText(this);
            alert.setView(input);
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Editable value = input.getText();
                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Canceled.
                }
            });

            alert.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
    }

    private Drawable.Callback drawableCallback = new Drawable.Callback() {
        @Override
        public void invalidateDrawable(Drawable who) {
            getActionBar().setBackgroundDrawable(who);
        }

        @Override
        public void scheduleDrawable(Drawable who, Runnable what, long when) {
            handler.postAtTime(what, when);
        }

        @Override
        public void unscheduleDrawable(Drawable who, Runnable what) {
            handler.removeCallbacks(what);
        }
    };

    public class MyPagerAdapter extends FragmentPagerAdapter{
        private final String[] TABS = {"Today", "This Week"};

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

        public CharSequence getPageTitle(int position){
            return TABS[position];
        }

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

        @Override
        public Fragment getItem(int position) {
            return WeatherTabs.newInstance(position);
        }
    }
}

activity_start.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        app:pstsShouldExpand="true" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs"
        tools:context=".Start" />
</RelativeLayout>

正如您在Start.java中看到的,我已经尝试在onCreate()方法中使用.setTypeface()并且它不会产生任何错误,但是当我运行我的应用程序时它在启动时崩溃所以我没有使用.setTypeface()正确。

4 个答案:

答案 0 :(得分:1)

在您的活动中添加以下代码:

com.viewpagerindicator.TabPageIndicator indicator = (com.viewpagerindicator.TabPageIndicator) findViewById(R.id.tabs);
    Typeface type = Typeface.createFromAsset(getAssets(),"fonts/BYekan+ Bold.ttf"); 
    LinearLayout view = (LinearLayout) indicator.getChildAt(0);
    int tabCount=3;
    for(int i=0;i<tabCount;i++){
        TextView textView = (TextView) view.getChildAt(i);
        textView.setTypeface(type);
    }

答案 1 :(得分:0)

如果您使用此库:

https://github.com/jpardogo/PagerSlidingTabStrip

在PagerSlidingTabStrip类更改行185:

mTabTextTypeface = Typeface.create(tabTextTypefaceName, mTabTextTypefaceStyle);

到此:

mTabTextTypeface = Typeface.create(Typeface.createFromAsset(context.getAssets(),"DroidNaskh.ttf"), mTabTextTypefaceStyle);

答案 2 :(得分:-1)

一段时间我遇到了这个问题 - 没有覆盖ViewPager代码就没有办法做到这一点

答案 3 :(得分:-1)

我使用了PagerSlidingTabStrip lib,我这样设置并且它适用于我

public void changeTabsFont( PagerSlidingTabStrip tabs)
{
    try
    {
        Typeface tab_face;
        tab_face = Typeface.create(Typeface.createFromAsset(activity.getAssets(), "font/NanumBarunGothic.ttf"), Typeface.NORMAL);
        ViewGroup vg = (ViewGroup) tabs.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(tab_face, Typeface.NORMAL);
                }
            }
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}