如何自定义工具栏标题字体,我需要给工具栏标题两个不同的字体。

时间:2017-04-05 06:28:54

标签: android android-toolbar android-typeface

假设我的标题是“附近 - 朋友”,我想将其显示为“附近 - 友“

我怎样才能实现它。

我累了很多但我做不到。

IMAGE REFERENCE

这是我使用的代码,我使用Typeface

TextView text = (TextView) tab.getCustomView();
String subTitle = text.getText().toString().trim();
text.setTextColor(getResources().getColor(R.color.white));
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");
text.setTypeface(face);
TextView title = new TextView(getApplicationContext());
title.setText("Nearby - ");
title.setTypeface(face);
if (subTitle.equals("NEAR BY")) {
    subTitle = "People";
} else if (subTitle.equals("FRIENDS")) {
    subTitle = "Friends";
} else if (subTitle.equals("FAMILY")) {
    subTitle = "Family";
}
toolbar.setTitle(title.getText().toString() + subTitle);

3 个答案:

答案 0 :(得分:3)

试一下

  

假设myToolBar是xml中的工具栏布局。

Toolbar toolBar = (Toolbar) findViewById(R.id.myToolbar);
setSupportActionBar(toolBar);
  

然后添加以下代码

String title = "<b>Near by-</b>Friends"
getSupportActionBar().setTitle(Html.fromHtml(title));

答案 1 :(得分:0)

您可以使用 SpannableString为TextView或其他启用文本的组件中的单个字符串句子设置不同的字体样式

String strMessage="Nearby- Friends";
Spannable spannable = new SpannableString(strMessage);

//"Nearby-" bold font
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.BLACK),Typeface.BOLD), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannable);


//"Friends" normal font
spannable.setSpan(getTextAppearanceSpan(ContextCompat.getColor(mContext, android.R.color.BLACK),Typeface.NORMAL), 8, strMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannable);

TextAppearanceSpan 方法,以自定义文字颜色

/*
 *get customized text with color, style
 */
private TextAppearanceSpan getTextAppearanceSpan(int color, int fontStyle) {
    ColorStateList blueColor1 = new ColorStateList(new int[][]{new int[]{}}, new int[]{color});
    return new TextAppearanceSpan(null, fontStyle, -1, blueColor1, null);
}

答案 2 :(得分:0)

如果您想在TextView中使用两种不同的字体(例如工具栏标题),则应使用Spannable将字体应用于文本的每个部分。

以下解决方案将从您的资源中应用两种自定义字体。

String title = "Nearby- ";
String subtitle = "Friends";
Typeface typefaceBold =  Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");
Typeface typefaceNormal =  Typeface.createFromAsset(getAssets(), "fonts/Roboto.ttf");
SpannableString spannableTitle = new SpannableString(title + subtitle);
spannableTitle.setSpan (new BetterTypefaceSpan(typefaceBold), 0, title.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spannableTitle.setSpan (new BetterTypefaceSpan(typefaceNormal), title.length(), spannableTitle.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
toolbar.setText(spannableTitle);

BetterTypefaceSpan.java

// Copy of CalligraphyTypefaceSpan
// https://github.com/chrisjenx/Calligraphy/blob/df1c07f82926831a5c47443bc49f9ff718a4c700/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyTypefaceSpan.java

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;

public class BetterTypefaceSpan extends MetricAffectingSpan {
    private final Typeface typeface;

    public BetterTypefaceSpan(final Typeface typeface) {
        if (typeface == null) {
            throw new IllegalArgumentException("typeface is null");
        }

        this.typeface = typeface;
    }

    @Override
    public void updateDrawState(final TextPaint drawState) {
        apply(drawState);
    }

    @Override
    public void updateMeasureState(final TextPaint paint) {
        apply(paint);
    }

    private void apply(final Paint paint) {
        final Typeface oldTypeface = paint.getTypeface();
        final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0;
        final int fakeStyle = oldStyle & ~typeface.getStyle();

        if ((fakeStyle & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fakeStyle & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(typeface);
    }
}