无法更改小部件颜色

时间:2014-09-02 20:08:55

标签: android android-widget

我想更改我的小部件的背景颜色,但它不起作用(颜色不会改变),我不知道为什么。

在我的配置活动中,我有一个更改颜色的按钮,例如:

button_black_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                background_color_int = 0x000000;

                //set color in widget-preview
                widget_background_preview.setBackgroundColor(background_color_int | (background_opacity_int << 24));

            }
        });

更改不透明度的第二个按钮:

button_bg_alpha_50.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                background_opacity_int = 0x80;
                widget_background_preview.setBackgroundColor(background_color_int | (background_opacity_int << 24));

            }
        });

最后,'接受'按钮:

    button_accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = getIntent();
                    Bundle extras = intent.getExtras();

                    if (extras != null) {
                        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                AppWidgetManager.INVALID_APPWIDGET_ID);
                    }

                    remoteViews.setInt(R.id.widget_relative, "setBackgroundColor", background_color_int | (background_opacity_int << 24));

                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
                appWidgetManager.updateAppWidget(mAppWidgetId, remoteViews);

                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

                setResult(RESULT_OK, resultValue);
                finish();

1 个答案:

答案 0 :(得分:0)

小部件是RemoteView。您有非常有限的资源来更新窗口小部件的UI,您不应该直接执行它。

您可以尝试这样的事情:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                          R.layout.your_layout);
remoteViews.setInt(viewId, "setBackgroundColor", Color.BLACK);//this line for changing background colour 

Color.BLACK更改为您选择的颜色(整数值)。