我還沒有找到辦法做到這一點。可能嗎?如何使用TypefaceSpan或StyleSpan和自定義字體?
60
A
回答
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);
}
}
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);
}
相關問題
- 1. Xamarin Android TypefaceSpan在Assets文件夾中具有自定義字體
- 2. 使用TypefaceSpan設置ActionBar標題字體
- 3. 如何使用CSS自定義字體?
- 4. 如何使用自定義CSS字體?
- 5. 如何在自定義BaseAdapter中使用自定義字體
- 6. 使用自定義字體
- 7. 使用自定義字體
- 8. 如何在Xamarin.Android中使用自定義字體?字體問題
- 9. 如何定義使用自定義字體的樣式
- 10. 如何正確使用UGUI掩碼和自定義字體
- 11. 自定義字體和字體平滑
- 12. 在Android上使用或不使用自定義字體
- 13. 用sweave和beamer自定義ggplot字體
- 14. 如何在可搜索字典中使用自定義字體?
- 15. 如何使UIAlertView與自定義字體,高度和寬度?
- 16. 如何在AndEngine中使用斜體或粗體字體創建自定義字體?
- 17. 如何將自定義ListViews字體更改爲自定義字體?
- 18. 自定義字體
- 19. 自定義字體
- 20. 自定義字體
- 21. 自定義字體?
- 22. 使用自定義字體的自定義textview
- 23. 後webview使用自定義字體
- 24. node-canvas:使用自定義字體
- 25. 在UIWebView中使用自定義字體
- 26. $ .WIDTH()使用自定義字體時
- 27. webview使用的自定義字體
- 28. 在css中使用自定義字體?
- 29. 使用WiX擴展自定義字體
- 30. 使用自定義字體的問題
不知怎的,這不會對按鈕的作用。任何想法爲什麼? – mikepenz 2014-11-02 10:25:54
@notme我應該傳遞給此構造函數中的字符串變量族CustomTypefaceSpan(String family,Typeface type){} ??? – KJEjava48 2017-03-08 11:27:10