viewPager中的自定义字体

时间:2014-09-28 08:49:04

标签: android android-viewpager

我正在尝试在viewPager上使用自定义字体(标签标题的自定义字体)。下面是我用来启动标签和片段的代码。我在google搜索了最近4个小时,但没有任何东西符合我的想法。基本上我想使用我在assets文件夹中的自定义字体。我怎样才能将这个实现到我拥有的代码中。我面临的主要问题是我无法在.setTypeface上使用view。注意:下面的代码没有问题。

MainActivity.java

public class MainActivity extends FragmentActivity implements ActionBar.TabListener, View.OnClickListener{

    //tabs
    public static ViewPager viewPager;
    private PagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = {"Home", "Messages", "Profile", "Logout"};

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //other code here...

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new AdapterManager(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener((ActionBar.TabListener) this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            @Override
            public void onPageSelected(int position){actionBar.setSelectedNavigationItem(position);}
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2){}
            @Override
            public void onPageScrollStateChanged(int arg0){}
        });
   }
}

AdapterManeger.java

public class AdapterManager extends FragmentPagerAdapter {
    public AdapterManager(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                return new HomeFragment();
            case 1:
                return new MessageFragment();
            case 2:
                return new ProfileFragment();
            case 3:
                return new LogoutFragment();
        }

        return null;
    }
    @Override
    public int getCount() {
        return 4;
    }
}

activity_main.xml中

<FrameLayout
    android:id="@+id/mainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>


</FrameLayout>

更新: 我试过这个:

Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome.ttf");
View view = (View) findViewById(R.id.viewLayout);
view.setTypeface(font);

<FrameLayout
    android:id="@+id/mainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <view
        android:id="@+id/viewLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>

    </view>



</FrameLayout>

0 个答案:

没有答案