问题 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);

1516
2018-01-21 10:11


起源

你好,我想知道你的解决方案。无论你是否得到解决方案? - iDroid Explorer
看看我的答案..它对我有用。 - Puru Pawar


答案:


好的 。我自己发现了一些关于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);
}

9
2018-02-04 07:15





尝试这个:

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);

2
2018-01-21 10:17



@ Archie.bgpc但我想在操作栏sherlock中添加Roboto字体到导航标签.....任何建议 - user1526671
您也可以在SpannableString对象中设置自定义字体。看看这个 所以 岗位 - Anand Sainath


要解决此问题,您可以创建一个特殊操作栏类。 只需创建一个类 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
2018-01-21 10:17





首先创建以下自定义 TypefaceSpan 你的project.Bit更改版本的类 自定义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);

我的答案的原始灵感是:使用自定义字体设置Android操作栏标题的样式


2
2018-05-15 13:25





好像你没有得到任何指导。我不确定我的答案的结果,但是你可以挑衅地想出你想要的答案。

让我试着帮助你。我认为你必须使用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>

你也可以参考 这个SO

现在只需将该主题应用于您的活动,您将获得所需的TabText字体。

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

享受编码... :)

在EXTRA

另一种解决方案可能是修改选项卡图像以包含文本 GIMP 或photoshop的东西,然后只是在选项卡中设置这些图像,而不是图像和文本。这是一种尴尬的方式,但它会起作用。

希望这可以帮助!


1
2018-02-08 05:50



这不是一个好的解决方案。如果以编程方式生成选项卡文本怎么办? - giorgiline


你可以这样做:

// 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);

0
2018-02-24 01:34