2013-10-31 178 views
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) { 

} 

}

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

回答

1

这里有一个efficent的方式来改变字体为整个应用程序:Access a typeface once from asset and use it as a reference

编辑


您可以使用源代码我将你引向上面的链接。现在你可以做的是从父布局开始递归地改变字体,并且为他们的每个子节点递减。您应该知道,任何可以包含文本的视图都是直接或间接的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); 
     } 
    } 
+0

我没有使用textview。我正在使用sherlockactionbar碎片。它不工作。 – Shruthi

+0

请检查我刚刚做的版本。如果您认为答案对您有帮助,请标记为关闭该线程的正确答案:) –