簡體   English   中英

Android:如何在App Widget中設置自定義字體?

[英]Android: how to set custom font in App Widget?

嗨,謝謝你的幫助。

我需要在我的App Widget中使用我的自定義字體。

通常我會使用類似的東西:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "myfont.ttf");  
txt.setTypeface(font);

但是In不能在RemoteViews上調用setTypeface(font)

請問有什么建議嗎?

謝謝!

您可以使用以下方法。

將字體渲染到畫布上,然后將其傳遞給位圖並將其指定給ImageView。

public Bitmap buildUpdate(String text) 
{
    Bitmap myBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    Typeface mytypeface = Typeface.createFromAsset(this.getAssets(),"fontname.ttf");
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTypeface(clock);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(65);
    paint.setTextAlign(Align.CENTER);
    myCanvas.drawText(text, 80, 60, paint);
    return myBitmap;
}

使用它像:

String text = "This is my text";
RemoteViews views = new RemoteViews(getPackageName(), R.layout.my_widget_layout);
views.setImageViewBitmap(R.id.my+imageview, buildUpdate(text));

希望這會有所幫助:)

您無法執行此操作,因為應用程序在不同的進程中運行,因此您只能使用系統字體。

(我知道用戶在很久以前發布了這個帖子,但答案可能對遇到它的其他人有用)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM