Android does not support layout XML to directly use TTF and other custom fonts.
The Custom font must be placed in the asset directory and context must be called. the getassets () method is used to obtain the resources of custom fonts. Because Android widgets depend on other processes, fonts cannot be used in widgets. the settypeface method. Therefore, you can use the Custom font in the widget to convert the font to the image output, and then use views. the setimageviewbitmap method can implement custom functions. Refer to the Code:
Static bitmap buildupdate (string time, context ){
Bitmap mybitmap = bitmap. createbitmap (240, 80, bitmap. config. argb_4444 );
Canvas mycanvas = new canvas (mybitmap );
Paint paint = new paint ();
Typeface TF = typeface. createfromasset (context. getassets (), "fonts/clockopia. TTF ");
Paint. setantialias (true );
Paint. setalpha (110); // value range: 0 ~ 255, the smaller the value, the more transparent
Paint. setsubpixeltext (true );
Paint. settypeface (TF );
Paint. setstyle (paint. style. Fill );
Paint. setcolor (color. White );
Paint. settextsize (80 );
Paint. settextalign (align. center );
Mycanvas. drawtext (time, 100, 60, paint );
Return mybitmap;
}
// Call the display time desktop widget in the widget
Remoteviews mviews = new remoteviews (context. getpackagename (), R. layout. Main );
Mviews. setimageviewbitmap (R. Id. imageview1, buildupdate (time, context ));
PS: Because the paint brush and image are used for font, <imageview> is used for layout.
PPS:
Widgets only support simple components, such as imageview and textvew. They do not support advanced and complex components, such as listview and edittext. However, widgets can click to trigger an activity that contains any components. The procedure is as follows:
[HTML] the Code must be placed in the onupdate () method for execution.
Remoteviews (context. getpackagename (), R. layout. Main );
Intent intent = new intent (context, called activity. Class );
Pendingintent pend = pendinginent. getactivity (context, 0, intent, 0 );
Mremoteviews. setonclickpendingintent (R. Id. widget, pend );.