简体   繁体   English

如何通过单击图像按钮更改小部件背景

[英]How to change widget background by clicking an image button

Right I've looked at a lot of questions, answers and tutorials but none have helped me so my question is my last hope... 是的,我看了很多问题,答案和教程,但没有一个对我有帮助,所以我的问题是我最后的希望...

On my clock widgets config activity i want to be able to have image button so that the user can change the background on the config menu, like it is on the HD Widgets app, but with a different background of course and so far i have this 在我的时钟小部件配置活动中,我希望能够具有图像按钮,以便用户可以像在HD Widgets应用程序上一样在配置菜单上更改背景,但是当然背景不同,到目前为止,我拥有

 @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.widget_config);
            setResult(RESULT_CANCELED);
 }

    int mAppWidgetId;
    @SuppressWarnings({ "unused" })
    private void showAppWidget() {

       mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
       Intent intent = getIntent();
       Bundle extras = intent.getExtras();
       if (extras != null) {
           mAppWidgetId = extras.getInt(EXTRA_APPWIDGET_ID,INVALID_APPWIDGET_ID);
           final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
           button1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
        button1.setImageResource(R.drawable.background3);
            AppWidgetProviderInfo providerInfo = AppWidgetManager.getInstance(
                                    getBaseContext()).getAppWidgetInfo(mAppWidgetId);
            String appWidgetLabel = providerInfo.label;
            Intent startService = new Intent(WidgetConfigActivity.this, null);
            startService.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId);
            startService.setAction("FROM CONFIGURATION ACTIVITY");
            setResult(RESULT_OK, startService);
            startService(startService);
            finish();
            if (mAppWidgetId == INVALID_APPWIDGET_ID) {
            Log.i("I am invalid", "I am invalid");
            finish();
            }
    }

From the answers I've looked at, you need to use RemoteViews but I'm not entirely sure how to implement that into my code so that where i need some help Thank you 从我看过的答案中,您需要使用RemoteViews,但是我不确定如何在代码中实现它,以便在需要帮助的地方谢谢。

You get the RemoteViews like this 你会得到这样的RemoteViews

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

Then you change background like this 然后像这样改变背景

remoteViews.setInt(R.id.some_view, "setBackgroundResource", R.drawable.my_color);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM