更改窗口小部件中的textview字体?

RED*_*ED_ 1 java fonts android widget

有没有办法改变小部件中的字体?我在这里看到了一些问题/答案,但有些人说它无法完成.其他人说可以这样做,但不提供太多信息.我说的是我放在资源文件夹中的自定义字体.我想要使​​用的字体是设计指南中的Roboto.

我主要想问,因为其他问题说明不可能.我的Nexus 7上有Google Currents小部件,它在小部件上使用Roboto字体,所以肯定有可能吗?

我想改变的TextView很好,只是一个textview.目前它没有格式/样式.我唯一需要的是将字体改为Roboto light.

我很感激有关如何做到这一点的一些建议.谢谢.

我实际上计划开源我在这里制作的小部件,所以它也可以帮助其他人.

Ahm*_*mad 9

有没有办法改变小部件中的字体?

是的,但不幸的是你只能使用系统字体.你可以在Android 4.1+上原生使用Roboto字体,但是:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
Run Code Online (Sandbox Code Playgroud)

在以前版本的Text上使用Roboto的唯一方法是创建一个Bitmap并在Canvas上绘制Text,如下所示:

    Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/myFont.ttf");

    paint.setTypeface(font);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(...);
    paint.setTextSize(...);
    myCanvas.drawText("Hello World", x, y, paint);
Run Code Online (Sandbox Code Playgroud)

然后将它设置为RemoteView:

rViews.setImageViewBitmap(R.id.myImageView, myBitmap);
Run Code Online (Sandbox Code Playgroud)