如何在小部件的列表视图中以编程方式设置项目的背景颜色?
How to set background color for an item programmatically in listview in a widget?
我按照 github 上的示例代码制作了 Widget
: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"
这样的字符串形式
----编辑----
为了更好地理解问题,数据提供程序中没有这样的方法来设置小部件的背景颜色class:
public class LoremViewsFactory implements RemoteViewsService.RemoteViewsFactory {
@Override
public RemoteViews getViewAt(int position) {
// no method available for setting background color
请检查这个更新的答案:-
在定义主要小部件布局的布局中,我必须添加以下内容: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);
我在 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);
}
我按照 github 上的示例代码制作了 Widget
: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"
----编辑----
为了更好地理解问题,数据提供程序中没有这样的方法来设置小部件的背景颜色class:
public class LoremViewsFactory implements RemoteViewsService.RemoteViewsFactory {
@Override
public RemoteViews getViewAt(int position) {
// no method available for setting background color
请检查这个更新的答案:-
在定义主要小部件布局的布局中,我必须添加以下内容: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);
我在 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);
}