0

如何自定义标签片段上的 ttf 字体?我正在使用sherlockactionbar。这是我的代码,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

<FrameLayout
    android:id="@+id/ptr_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

在主要活动中使用,

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

    /** to check whether user logged in or not */
    session = new TwitterHomeActivity(getApplicationContext());
    session.checklogin();

    /** Getting a reference to action bar of this activity */
    mActionBar = getSupportActionBar();

    getSupportActionBar().setDisplayUseLogoEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mActionBar.setDisplayOptions(0, ActionBar.DISPLAY_USE_LOGO);

    /** Set tab navigation mode */
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    /** Getting a reference to ViewPager from the layout */
    mPager = (ViewPager) findViewById(R.id.pager);

    /** Getting a reference to FragmentManager */
    FragmentManager fm = getSupportFragmentManager();

    /** Defining a listener for pageChange */
    ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            setTitle(getTitleFromPosition(position));
            mActionBar.setSelectedNavigationItem(position);
            // mActionBar.setBackgroundDrawable(R.drawable.ic_action_chance_icon);
        }

        private CharSequence getTitleFromPosition(int position) {
            if (position == 0) {
                title = "text";
            } else if (position == 1) {
                title = "text";
            } else if (position == 2) {
                title = "text";
            } else if (position == 3) {
                title = "text";
            }
            return title;
        }
    };

    /** Setting the pageChange listner to the viewPager */
    mPager.setOnPageChangeListener(pageChangeListener);

    /** Creating an instance of FragmentPagerAdapter */
    MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
            fm);

    /** Setting the FragmentPagerAdapter object to the viewPager object */
    mPager.setAdapter(fragmentPagerAdapter);

    mActionBar.setDisplayShowTitleEnabled(true);

    /** Defining tab listener */
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {

            mPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {

        }
    };

    /**/

    Tab tab = mActionBar.newTab();
    tab = mActionBar.newTab();

    tab.setText(“text”);

    tab.setTabListener(tabListener);

    mActionBar.addTab(tab);

    tab = mActionBar.newTab();
    tab.setText(“ttext”);
    tab.setTabListener(tabListener);

    mActionBar.addTab(tab);

    tab = mActionBar.newTab();
    tab.setText(“text”);
    tab.setTabListener(tabListener);

    mActionBar.addTab(tab);

    tab = mActionBar.newTab();

    tab.setText(“text”);
    tab.setTabListener(tabListener);

    mActionBar.addTab(tab);

    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class
                .getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.camera:
        startActivity(new Intent(MainActivity.this, Request_Broadcast.class));
        overridePendingTransition(R.anim.right_slide_in,
                R.anim.right_slide_out);
        return true;

    case R.id.find_people:
        startActivity(new Intent(MainActivity.this, Find_people.class));
        overridePendingTransition(R.anim.right_slide_in,
                R.anim.right_slide_out);
        return true;
    case R.id.action_settings:
        startActivity(new Intent(MainActivity.this, Pro_settings.class));
        overridePendingTransition(R.anim.right_slide_in,
                R.anim.right_slide_out);
        return true;

    }
    return true;
}

@SuppressLint("NewApi")
public void setActionBarTitle(int feedPage) {
    getActionBar().setTitle(feedPage);
}

@Override
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {

}

}

现在我想使用字体自定义选项卡上的字体。如何做到这一点,选项卡上的文本以大写字母显示。我想用小写字母显示..

4

1 回答 1

1

在这里,您可以有效地更改整个应用程序的字体:从资产中访问一次字体并将其用作参考

编辑


您可以使用我向您推荐的课程的源代码,请参阅上面的链接。现在您可以做的是从父布局开始递归地更改字体,然后为每个子布局递减。您应该知道,任何可以包含文本的视图都直接或间接地是 TextView 类的子类(即 Button、EditText、CheckBox、CompoundButton、RadioButton、ToggleButton 等)。如果您想在您的应用程序中使用不同的字体,您可以使用例如视图的标签属性来定义将具有该特定视图的字体。示例布局如下所示:

activity_main.xml

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="title"
    android:text="This is the title screen" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="content"
    android:text="This is the activity content." />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="content"
    android:text="This is a button" />

下面是递归方法,您可以通过它更改活动的所有视图的字体。此方法是在类FontUtils中定义的附加方法:

公共静态无效 setActivityTypeFace(上下文上下文,ViewGroup viewGroup){

Object tagAux;

for (int i = 0; i < viewGroup.getChildCount(); i++) {

    View viewChild = viewGroup.getChildAt(i);

    if (viewChild instanceof ViewGroup) {
        setActivityTypeFace(context, (ViewGroup) viewChild);

    } else if (viewChild instanceof TextView) {

        tagAux = viewChild.getTag();

        if (tagAux != null) {

            if (((String) tagAux).compareTo("title") == 0) 
            {
                ((TextView) viewChild).setTypeface(getTypeface(context,
                        FontType.TITLE_FONT.toString()));
            }
        } else if (((String) tagAux).compareTo("content") == 0) {
            ((TextView) viewChild).setTypeface(getTypeface(context,
                    FontType.CONTENT_FONT.toString()));
        }
    }
}

}

MainActivity.java

   public class MainActivity extends Activity

        private ViewGroup mRootView;

        ...

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                mRootView = (ViewGroup) findViewById(R.id.rootView);
                ...
                FontUtil.setActivityTypeFace(this, mRootView);
        }
    }
于 2013-10-31T09:04:51.803 回答