ActionBarSherlock选项卡的自定义字体

时间:2013-01-21 10:11:29

标签: android actionbarsherlock

我想为ActionBarSherlock中的“视频”和“图片”标签设置字体。我使用以下代码来执行此操作。它在ICS中准确显示,但在较低版本的设备中没有显示,但我通过设置TYPE FACE就显示了该应用程序其他部分的准确输出...

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

但如何在此代码中的TypeFace中设置ActionBar

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

6 个答案:

答案 0 :(得分:9)

好的。我自己发现了一些在SO上的地方。

首先在其中创建一个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" />

然后,在您实例化ActionBar的类中,使用此代码在每个选项卡上设置文本。 (此示例使用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 :(得分:2)

要解决此问题,您可以创建一个特殊操作栏类。 只需创建一个类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 :(得分: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);

答案 3 :(得分:2)

首先在项目中创建以下自定义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);
    }

}

现在应用这样的代码:(我在我的一个Bangla应用程序上成功使用了这个)

        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

答案 4 :(得分: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字体。

如果有任何疑问,请评论我。

享受编码......:)

在EXTRA中

另一个解决方案可能是修改标签图像以包含文本,包含GIMP或photoshop的内容,然后只需在标签中设置这些图片,而不是图片和文字。这样做有点尴尬,但它会起作用。

希望这有帮助!

答案 5 :(得分: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);
相关问题