简体   繁体   中英

How to change font of textview in Home screen widget?

I want to change the font of textview in Home screen widget?

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.time_widget_layout);
        remoteViews.setTextViewText(R.id.tvTime, Utility.getCurrentTime("hh:mm:ss a"));

i have font in my assets folder.

TextView has a method on it called setTypeface

The code to set the font would look similar to this if your font is at "Assets/Font/Font.otf":

Typeface font = Typeface.createFromAsset(context.getAssets(), "Font/Font.otf");
TextView textView = (TextView)findViewById(R.id.tvTime);
textView.setTypeface(font, Typeface.NORMAL);

Optional: If needed, you can do this inside the constructor of a class inheriting from TextView, then use that class in your layouts, in case you wanted to use that font in a bunch of different places. Also, if you wanted to do it inside the constructor, you could have the name of the font passed in as a styled attribute in your layout and use that instead of the hardcoded string like above.

RemoteView methods are very limited. but setImageViewBitmap is enough.
create a TextView using java code, enable this features:

textView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
textView.setDrawingCacheEnabled(true);

now set typeFace,change font size and do whatever you want, at last you can get your desired Bitmap and set it to your home screen ImageView

textView.buildDrawingCache();
Bitmap finalResult = textView.getDrawingCache();
remoteImageView.setImageViewBitmap(R.id.ivWidget, finalResult);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM