Как установить цвет фона для элемента программно в виде списка в виджете? - PullRequest
0 голосов
/ 11 июля 2019

Я сделал Widget после примера кода на github: LoremWidget .

Я хочу установить разные цвета фона для каждого элемента (не случайно).Я хочу установить цвет фона чуть ниже линии:

row.setTextViewText(R.id.widget_row_title, items[position].title)
// I want to do something similar like:
// row.findViewById<LinearLayout>(R.id.widget_row_root).setBackgroundColor = ...

Цвет, который я хочу установить, имеет вид строки, такой как "#e57373"

---- EDIT---

Для лучшего понимания проблемы не существует такого метода для установки цвета фона в классе поставщика данных для виджета:

public class LoremViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    @Override
    public RemoteViews getViewAt(int position) {
        // no method available for setting background color

Ответы [ 2 ]

1 голос
/ 11 июля 2019

Пожалуйста, проверьте этот обновленный ответ: -

В макете, который определил макет основного виджета, мне нужно было добавить следующее: android: id = "@ + id / LineaRLayout1"

затемв AppWidgetProvider onUpdate в конечном итоге выглядело так: все, что я на самом деле показываю, это то, как я изменил цвет

, просто замените id элемента на id вашего элемента.

@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, WidgetActivity.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.activity_main);

        //try change color here with no luck, I tried activity_main,background, widget1

        views.setInt(R.id.LineaRLayout1, "setBackgroundColor", Color.GREEN);
        views.setOnClickPendingIntent(R.id.button, pendingIntent);
        // Tell the AppWidgetManager to perform an update on the current app
        // widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
0 голосов
/ 11 июля 2019

Я делаю это методом onUpdate

remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);

if(/*condition for changing background colour is met*/){
    remoteViews.setInt(R.id.widget_row_title, "setBackgroundColor", R.color.new_colour);
}else{
    remoteViews.setInt(R.id.widget_row_title, "setBackgroundColor", R.color.default_colour);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...