2011-01-27 20 views

回答

129

嗯,我無法弄清楚如何用可用的類來做到這一點,所以我自己擴展了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); 
    } 
} 
+2

不知怎的,這不會對按鈕的作用。任何想法爲什麼? – mikepenz 2014-11-02 10:25:54

+0

@notme我應該傳遞給此構造函數中的字符串變量族CustomTypefaceSpan(String family,Typeface type){} ??? – KJEjava48 2017-03-08 11:27:10

82

儘管notme基本上是正確的想法,但給出的解決方案有點怪異,因爲「family」變得多餘。它也有點不正確,因爲TypefaceSpan是Android知道的特殊跨度之一,並且期望與ParcelableSpan接口有關的特定行爲(該類別的子類不正確,也不可能實現)。

一種更簡單,更準確的解決辦法是:

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); 
    } 
} 
+1

+1謝謝!和[這裏](http://stackoverflow.com/a/10741161/89818)是一個正確的用法示例。 – caw 2014-07-24 04:37:02

-1

我嘗試了好幾種類似的解決方案,發現This是簡單可行。只需要將該項目單擊處理爲按鈕單擊而不是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); 
}