Actuellement j'apprends sur le widget dans Android. Auparavant, j'ai essayé d'implémenter un widget dans mon projet fictif et j'ai pu montrer TextView en couleur noire. Mais, maintenant j'ai un autre projet et j'ai implémenté le widget de la même manière. Je peux montrer le texte dans le widget, mais le problème est que la couleur du texte est blanche alors que j'utilise un fond blanc. Je ne pense pas que je modifie quelque chose dans mon projet précédent qui a rendu le texte noir. Suis-je manquer quelque chose ici, quelque chose comme passer le mauvais Context
peut-être?Pourquoi la couleur du texte dans TextView dans mon widget est-elle blanche?
Widget article:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" />
</LinearLayout>
RemoteViewsService:
public class WidgetItemService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new ItemFactory();
}
class ItemFactory implements RemoteViewsService.RemoteViewsFactory {
private List<String> mStrings;
public IngredientItemFactory() {
}
@Override
public void onCreate() {
}
@Override
public void onDataSetChanged() {
Cursor c = getContentResolver().query(StringContract.StringEntry.CONTENT_URI, null, null, null, null);
mStrings = CursorUtils.extract(c);
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
return mStrings.size();
}
@Override
public RemoteViews getViewAt(int pos) {
String string = mStrings.get(pos);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_item);
views.setTextViewText(R.id.tv_text, string);
return views;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
AppWidgetProvider:
public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId: appWidgetIds) {
String string = PrefUtils.load(this, appWidgetId);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent(context, WidgetItemService.class);
views.setRemoteAdapter(R.id.lv_list, intent);
views.setEmptyView(R.id.lv__list, R.id.tv_empty);
views.setTextViewText(R.id.tv_title, string);
Intent openActivity = BActivity.createIntent(string);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, openActivity, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
PrefUtils.delete(context, appWidgetId);
}
}
}
Vérifiez le thème de votre nouveau projet. –