【问题标题】:How to change widget background by clicking an image button如何通过单击图像按钮更改小部件背景
【发布时间】:2014-01-01 18:45:08
【问题描述】:

是的,我看过很多问题、答案和教程,但没有一个对我有帮助,所以我的问题是我最后的希望......

在我的时钟小部件配置活动中,我希望能够拥有图像按钮,以便用户可以更改配置菜单上的背景,就像它在 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();
            }
    }

从我看过的答案来看,您需要使用 RemoteViews,但我不完全确定如何在我的代码中实现它,以便我需要一些帮助 谢谢

【问题讨论】:

    标签: android android-widget widget imagebutton


    【解决方案1】:

    你会得到这样的 RemoteViews

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

    然后你像这样改变背景

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

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2017-03-07
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多