2012-02-21 37 views
6

我想將我的textview字體從Roboto定期更改爲roboto濃縮版。 textView位於Widget中,所以我使用的是RemoteView。如果它是一個應用程序,我們可以通過typeFace來設置它。我需要爲此做些什麼?從Roboto經常更改字體到Roboto濃縮

回答

3

我有答案了。我們要做的是將字體渲染到畫布上,然後將其傳遞給位圖並將其分配給圖像視圖

public Bitmap buildUpdate(String time) 
{ 
Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444); 
Canvas myCanvas = new Canvas(myBitmap); 
Paint paint = new Paint(); 
Typeface clock = Typeface.createFromAsset(this.getAssets(),"robonto_condunced.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(time, 80, 60, paint); 
return myBitmap; 
} 
1

你只是使用字體。下面是一個例子

private void setFonts() { // Setting all fonts 
    Typeface face = Typeface.createFromAsset(this.getAssets(), 
      "fonts/DroidSerif-Bold.ttf"); 
    mMonthTextView.setTypeface(face); 
    mAgeTextView.setTypeface(face); 
    mHeightAndWeightTextView.setTypeface(face); 

} 

你必須把該字體的資產/字體/文件夾

+0

我正在使用RemoteView作爲控件。所以我不能直接在我的代碼中獲取textview。我們不能直接在xml中設置字體類型嗎? – Kamalone 2012-02-21 11:11:35

+0

我認爲這是不可能的。我們只能將樣式改爲粗體或斜體。如果你想在XML中做到這一點,你必須創建一個自定義的文本視圖。但這需要一段時間才能完成。 – 2012-02-21 11:15:02