我怎样才能使用TypefaceSpan或StyleSpan与自定义字体?

我还没有find办法做到这一点。 可能吗?

那么我不知道如何用可用的类来做到这一点,所以我扩展了我自己的TypefaceSpan ,现在它适用于我。 这是我做的:

 package de.myproject.text.style; import android.graphics.Paint; import android.graphics.Typeface; import android.text.TextPaint; import android.text.style.TypefaceSpan; public class CustomTypefaceSpan extends TypefaceSpan { private final Typeface newType; public CustomTypefaceSpan(String family, Typeface type) { super(family); newType = type; } @Override public void updateDrawState(TextPaint ds) { applyCustomTypeFace(ds, newType); } @Override public void updateMeasureState(TextPaint paint) { applyCustomTypeFace(paint, newType); } private static void applyCustomTypeFace(Paint paint, Typeface tf) { int oldStyle; Typeface old = paint.getTypeface(); if (old == null) { oldStyle = 0; } else { oldStyle = old.getStyle(); } int fake = oldStyle & ~tf.getStyle(); if ((fake & Typeface.BOLD) != 0) { paint.setFakeBoldText(true); } if ((fake & Typeface.ITALIC) != 0) { paint.setTextSkewX(-0.25f); } paint.setTypeface(tf); } } 

虽然notme基本上是正确的想法,但是给出的解决scheme有点“hacky”,因为“family”变得多余。 它也有点不正确,因为TypefaceSpan是Android知道的特殊跨度之一,并且期望对于ParcelableSpan接口(该子类不正确,也不可能实现)的特定行为。

更简单和更准确的解决scheme是:

 public class CustomTypefaceSpan extends MetricAffectingSpan { private final Typeface typeface; public CustomTypefaceSpan(final Typeface typeface) { 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); } } 

我尝试了几个类似的解决scheme,发现这是简单和可行的。 只是项目点击处理作为button点击,而不是onOptionsItemSelected。 谢谢!

这是我的项目的代码:

在我的menu_main.xml中:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_friends" android:orderInCategory="100" android:title="@string/hello_world" app:actionViewClass="android.widget.Button" app:showAsAction="always" /> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="never" /> </menu> 

在我的MainActivity.java中:

 @Override public boolean onCreateOptionsMenu(Menu menu) { //Use custom menu MenuInflater inflater = getMenuInflater(); //Inflate the custom menu inflater.inflate(R.menu.menu_main, menu); //reference to the item of the menu MenuItem i=menu.findItem(R.id.action_friends); Button itemuser =(Button) i.getActionView(); if(itemuser!=null){ // Create Typeface object to use unicode font in assets folder Typeface a = Typeface.createFromAsset(getApplicationContext(), "fontawesome-webfont.ttf"); // Set unicode font to menu item itemuser.setTypeface(a); itemuser.setText(getString(R.string.fa_users)); // Set item text and color itemuser.setTextColor(Color.BLACK); // Make item background transparent itemuser.setBackgroundColor(Color.TRANSPARENT); itemuser.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //set action when clicked } }); } return super.onCreateOptionsMenu(menu); } 
Interesting Posts