Java 具有自定义字体的Android小部件
我正在尝试创建一个简单的android小部件,显示我想在我的应用程序中使用自定义字体,有人能帮我吗 这是我的代码:Java 具有自定义字体的Android小部件,java,android,android-layout,android-activity,Java,Android,Android Layout,Android Activity,我正在尝试创建一个简单的android小部件,显示我想在我的应用程序中使用自定义字体,有人能帮我吗 这是我的代码: public class ExampleAppWidgetProvider extends AppWidgetProvider { private static final String LOG_TAG = "ExampleWidget"; private static final DateFormat df = new SimpleDateFormat("hh:m
public class ExampleAppWidgetProvider extends AppWidgetProvider {
private static final String LOG_TAG = "ExampleWidget";
private static final DateFormat df = new SimpleDateFormat("hh:mm:ss");
/**
* Custom Intent name that is used by the AlarmManager to tell us to update
* the clock once per second.
*/
public static String CLOCK_WIDGET_UPDATE = "com.eightbitcloud.example.widget.8BITCLOCK_WIDGET_UPDATE";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
Log.d(LOG_TAG, "Clock update");
// Get the widget manager and ids for this widget provider, then
// call the shared
// clock update method.
ComponentName thisAppWidget = new ComponentName(
context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID : ids) {
updateAppWidget(context, appWidgetManager, appWidgetID);
}
}
}
private PendingIntent createClockTickIntent(Context context) {
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.d(LOG_TAG, "Widget Provider disabled. Turning off timer");
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(createClockTickIntent(context));
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d(LOG_TAG,
"Widget Provider enabled. Starting timer to update widget every second");
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
1000, createClockTickIntent(context));
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.d(LOG_TAG, "Updating Example Widgets.");
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, WidgetExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget1);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
// Update The clock label using a shared method
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
public static void updateAppWidget(Context context,
AppWidgetManager appWidgetManager, int appWidgetId) {
String currentTime = df.format(new Date());
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.widget1);
updateViews.setTextViewText(R.id.widget1label, currentTime);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
}
公共类示例AppWidgetProvider扩展AppWidgetProvider{
私有静态最终字符串日志\u TAG=“ExampleWidget”;
私有静态最终日期格式df=新的SimpleDataFormat(“hh:mm:ss”);
/**
*AlarmManager用来告诉我们更新的自定义意图名称
*时钟每秒响一次。
*/
公共静态字符串CLOCK_WIDGET_UPDATE=“com.eightbitcloud.example.WIDGET.8BITCLOCK_WIDGET_UPDATE”;
@凌驾
公共void onReceive(上下文、意图){
super.onReceive(上下文、意图);
if(CLOCK\u WIDGET\u UPDATE.equals(intent.getAction())){
Log.d(Log_标签,“时钟更新”);
//获取此小部件提供程序的小部件管理器和ID,然后
//调用共享
//时钟更新方法。
ComponentName thisAppWidget=新组件名称(
context.getPackageName(),getClass().getName());
AppWidgetManager AppWidgetManager=AppWidgetManager
.getInstance(上下文);
int-id[]=appWidgetManager.getAppWidgetIds(thisAppWidget);
for(int-appWidgetID:ids){
updateAppWidget(上下文、appWidgetManager、appWidgetID);
}
}
}
私有PendingEvent createClockTickIntent(上下文){
意向意向=新意向(时钟小部件更新);
PendingEvent PendingEvent=PendingEvent.getBroadcast(上下文,0,
意向、挂起内容、标志(更新当前);
返回悬挂式帐篷;
}
@凌驾
已禁用公共无效(上下文){
super.onDisabled(上下文);
Log.d(Log_标记,“禁用小部件提供程序。关闭计时器”);
AlarmManager AlarmManager=(AlarmManager)上下文
.getSystemService(Context.ALARM\u服务);
alarmManager.cancel(createClockTickIntent(上下文));
}
@凌驾
公共void已启用(上下文){
super.onEnabled(上下文);
Log.d(Log_标签,
“小部件提供程序已启用。启动计时器以每秒更新小部件”);
AlarmManager AlarmManager=(AlarmManager)上下文
.getSystemService(Context.ALARM\u服务);
日历=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(calendar.SECOND,1);
alarmManager.setRepeating(alarmManager.RTC,calendar.getTimeInMillis(),
1000,createClockTickIntent(上下文));
}
@凌驾
public void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,
int[]appWidgetIds){
final int N=appWidgetIds.length;
d(Log_标记,“更新示例小部件”);
//对属于此应用程序的每个应用程序小部件执行此循环过程
//提供者
对于(int i=0;i
请编辑您的标题。所以这个问题可能会有所帮助。