【问题标题】:How to customise toolbar title font, I need to give two different fonts to Toolbar title. [duplicate]如何自定义工具栏标题字体,我需要给工具栏标题两种不同的字体。 [复制]
【发布时间】:2017-04-06 12:22:53
【问题描述】:

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

我怎样才能实现它。

我很累,但我做不到。

这里是我使用的代码,我使用字体

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

【问题讨论】:

  • 欢迎来到 SO。提问前请阅读如何使用minimal reproducible example提问
  • 但我认为问题与工具栏有关,而不是与 TextView 直接有关。那么它是如何重复的呢?

标签: android android-toolbar android-typeface


【解决方案1】:

试试这个

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

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

然后添加下面的代码

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

【讨论】:

  • 看来可以,让我试一次
  • 也试着看看@Kishore ans。使用 Spannable 也可以实现你已经写了一些额外的行。
【解决方案2】:

您可以使用 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);
}

【讨论】:

    【解决方案3】:

    如果您想在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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多