2013-01-21 67 views
11

我想爲ActionBarSherlock中的「視頻」和「圖像」選項卡設置字體。我已經使用下面的代碼來做到這一點。它準確地顯示在ICS,但不是在低版本的設備,但我已經通過設置TYPE臉像顯示在此應用程序的其它部分精確的輸出...ActionBarSherlock標籤的自定義字體

a.settypeface("ab.tttf"); 
a.settext("VIDEO"); 

但如何做我的設置TypeFaceActionBar在此代碼中:

mTabsAdapter.addTab(bar.newTab().setText("IMAGE"), AFragment.class, null); 
mTabsAdapter.addTab(bar.newTab().setText("VIDEO"), BFragment.class, null); 
+0

你好真的想知道你的解決方案。無論你是否得到解決方案? –

+0

看看我的答案..它爲我工作。 –

回答

2

要解決此問題,您可以創建一個特殊的Actionbar類。 只需創建一個類myBar extends Sherlockactionbar並放入settypeface。如果您現在在視圖中創建該欄,那麼它將具有字體。例如,這裏是一個帶有新字體的按鈕。

public class ChangedButton extends Button{ 

    public ChangedButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/viking2.TTF"); 
     this.setTypeface(font); 
    } 
} 

問候

2

試試這個:

String s = "VIDEO"; 
SpannableString mSS = new SpannableString(s); 
mSS.setSpan(new StyleSpan(Typeface.BOLD), 0, s.length(), 0); 
mTabsAdapter.addTab(bar.newTab().setText(mSS), 
       BFragment.class, null); 
+0

@ Archie.bgpc但我想添加Roboto字體到操作欄中的導航選項卡sherlock .....任何建議 – user1526671

+0

您也可以在SpannableString對象中設置自定義字體。看看這[SO](http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title)後 –

9

好。我發現它自己在一些地方。

首先要在其與此XML文件:tab_title.xml

<TextView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/action_custom_title" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="My Custom title" 
android:textColor="#fff" 
android:textSize="18sp" 
android:paddingTop="5dp" /> 

然後在類,你在實例化你的動作條使用此代碼來設置每個標籤的文本。 (這個例子是使用ActionBarSherlock。)

ActionBar bar = getSupportActionBar(); 
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

String[] tabNames = {"Tab 1","Tab 2","Tab 3"}; 

for(int i = 0; i<bar.getTabCount(); i++){ 
    LayoutInflater inflater = LayoutInflater.from(this); 
    View customView = inflater.inflate(R.layout.tab_title, null); 

    TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title); 
    titleTV.setText(tabNames[i]); 
    //Here you can also add any other styling you want. 

    bar.getTabAt(i).setCustomView(customView); 
} 
1

看起來你似乎沒有得到任何指導。我不確定我的答案的結果,但是是的,你可以根據自己的意願挑戰自己的想法。

讓我試着幫你。我認爲你必須有點玩android sdk的內置默認資源。

你必須創建自定義樣式:

<style name="MyActionBarTabText" parent="Widget.ActionBar.TabText"> 
    <item name="android:textAppearance">@style/TextAppearance.Holo.Medium</item> 
    <item name="android:textColor">?android:attr/textColorPrimary</item> 
    <item name="android:textSize">12sp</item> 
    <item name="android:textStyle">bold</item> 
    <item name="android:textAllCaps">true</item> 
    <item name="android:ellipsize">marquee</item> 
    <item name="android:maxLines">2</item> 
</style> 

<style name="Widget.ActionBar.TabText" parent="Widget"> 
    <item name="android:textAppearance">@style/TextAppearance.Widget.TextView.PopupMenu</item> 
    <item name="android:textColor">?android:attr/textColorPrimaryInverse</item> 
    <item name="android:textSize">18sp</item> 
    <item name="android:fontFamily">HERE YOU CAN GIVE YOUR FONT</item> 
</style> 

您也可以參考this SO

現在只需將該主題應用於您的活動,並且您將獲得所需的TabText字體。

評論我是否有任何疑問。

享受編碼... :)

額外

另一種解決辦法是修改標籤圖像包含的文本,用GIMP或Photoshop的東西,然後只需在設置這些圖像標籤,而不是圖像和文字。這樣做有點尷尬,但它會起作用。

希望這會有所幫助!

+0

這不是一個好的解決方案。如果標籤文本是以編程方式生成的呢? – giorgiline

2

首先創建下面你project.Bit定製TypefaceSpan類改變Custom TypefaceSpan版本,以使同時使用.otf.ttf字體。

import java.util.StringTokenizer; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.support.v4.util.LruCache; 
import android.text.TextPaint; 
import android.text.style.MetricAffectingSpan; 

public class TypefaceSpan extends MetricAffectingSpan{ 

    /*Cache to save loaded fonts*/ 
    private static LruCache<String, Typeface> typeFaceCache= new LruCache<String, Typeface>(12); 
    private Typeface mTypeface; 
    public TypefaceSpan(Context context,String typeFaceName) 
    { 
     StringTokenizer tokens=new StringTokenizer(typeFaceName,"."); 
     String fontName=tokens.nextToken(); 
     mTypeface=typeFaceCache.get(fontName); 

     if(mTypeface==null) 
     { 
      mTypeface=Constants.getFont(context, typeFaceName); 
      //cache the loaded font 
      typeFaceCache.put(fontName, mTypeface); 
     } 
    } 
    @Override 
    public void updateMeasureState(TextPaint p) { 
     p.setTypeface(mTypeface); 
    } 

    @Override 
    public void updateDrawState(TextPaint tp) { 
     tp.setTypeface(mTypeface); 
    } 

} 

現在應用這樣的代碼:(我用這對我的孟加拉的一個成功的應用程序)

 SpannableString mstKnwTitle=new SpannableString(getString(R.string.e_mustknow_tab)); 
     SpannableString cntctsTitle=new SpannableString(getString(R.string.e_number_tab)); 


     TypefaceSpan span=new TypefaceSpan(this, "solaimanlipi.ttf"); 

     mstKnwTitle.setSpan(span, 0, mstKnwTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
     cntctsTitle.setSpan(span, 0, mstKnwTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

     Tab tab= actionBar.newTab(); 
     tab.setText(mstKnwTitle); 
     tab.setTabListener(tabListener); 

     actionBar.addTab(tab); 


     tab= actionBar.newTab(); 
     tab.setText(cntctsTitle); 
     tab.setTabListener(tabListener); 

     actionBar.addTab(tab); 

我的答案的最初靈感是:Styling the Android Action Bar title using a custom typeface

0

你可以用這個做:

// Set a custom font on all of our ActionBar Tabs 
private boolean setCustomFontToActionBarTab(Object root) { 

    // Found the container, that holds the Tabs. This is the ScrollContainerView to be specific, 
    // but checking against that class is not possible, since it's part of the hidden API. 
    // We will check, if root is an instance of HorizontalScrollView instead, 
    // since ScrollContainerView extends HorizontalScrollView. 
    if (root instanceof HorizontalScrollView) { 
     // The Tabs are all wraped in a LinearLayout 
     root = ((ViewGroup) root).getChildAt(0); 
     if (root instanceof LinearLayout) { 
      // Found the Tabs. Now we can set a custom Font to all of them. 
      for (int i = 0; i < ((ViewGroup) root).getChildCount(); i++) { 
       LinearLayout child = ((LinearLayout)((ViewGroup) root).getChildAt(i)); 
       TextView actionBarTitle = (TextView) child.getChildAt(0); 
       Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "font/font.ttf"); 
       actionBarTitle.setTypeface(tf) 
      } 
      return true; 
     } 

    } 
    // Search ActionBar and the Tabs. Exclude the content of the app from this search. 
    else if (root instanceof ViewGroup) { 
     ViewGroup group = (ViewGroup) root; 
     if (group.getId() != android.R.id.content) { 
      // Found a container that isn't the container holding our screen layout. 
      // The Tabs have to be in here. 
      for (int i = 0; i < group.getChildCount(); i++) { 
       if (setCustomFontToActionBarTab(group.getChildAt(i))) { 
        // Found and done searching the View tree 
        return true; 
       } 
      } 
     } 
    } 
    // Found nothing 
    return false; 
} 

使用此呼叫:

ViewParent root = findViewById(android.R.id.content).getParent(); 
setCustomFontToActionBarTab(root); 
相關問題