Android设置小部件背景

时间:2011-02-06 08:23:55

标签: android background-image

在我的drawable-hdpi文件夹中,我有4个图像文件(.png)作为awidget的背景。默认情况下,为LinearLayout设置android:background =“@ drawable / goldgreenbg”。我创建了一个首选项屏幕,让用户更改背景。 怎么做?我想用这个代码:

      if (listpref.equals("color1"))
          {
          Toast.makeText(EditPreferences.this,  "Black" + listpref, Toast.LENGTH_LONG).show();

          }

else if (listpref.equals("color2"))
          {
              Toast.makeText(EditPreferences.this,  "Brown" + listpref, Toast.LENGTH_LONG).show();
          }

更新: 我应该把这段代码放在哪里? MainActivity.java:用于活动 UpdateService.java:用于小部件 EditPreferences.java:用于首选项 Main.xml包含listview,widgetlayout是它的id。

  setContentView(R.layout.main);
  preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String listpref = preferences.getString("listPref", "n/a");              
  LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout);
  if (listpref.equals("color1"))
  {
      Toast.makeText(MainActivity.this, "Black" + listpref, Toast.LENGTH_LONG).show();
      ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg));
  }
  else if (listpref.equals("color2"))
  {
      Toast.makeText(MainActivity.this, "Brown" + listpref, Toast.LENGTH_LONG).show();
      ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.brownbg));
  }

2 个答案:

答案 0 :(得分:1)

假设您的屏幕上已经有LinearLayout(使用setContentView),您可以很容易地更改背景:

yourLinearLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg));

(并使用findViewById() ofcourse获取该布局)

答案 1 :(得分:0)

我找到了解决方案。

EditPreferences.java:

final Preference listpref = getPreferenceScreen().findPreference("listPref");
        listpref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() 
        {
         public boolean onPreferenceChange(Preference p, Object newValue) 
         {
          String color = (String) newValue;

          if (color.equals("color1"))
          {
              RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(208, 202, 202));
              updateViews.setTextColor(R.id.widget_textview2, Color.WHITE);
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(176, 175, 175));
             // updateViews.setImageViewBitmap(R.id.ImageView01, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.forestbg)).getBitmap());
              updateViews.setImageViewResource(R.id.ImageView01, R.drawable.blacktrans);            
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);
 }
          else if (color.equals("color2"))
          {
              RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(23, 81, 11));
              updateViews.setTextColor(R.id.widget_textview2, Color.rgb(232, 232, 107));
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(23, 81, 11));
              updateViews.setImageViewBitmap(R.id.ImageView01, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.goldgreenbg)).getBitmap());
              // updateViews.setImageViewResource(R.id.ImageView01, R.drawable.goldgreenbgf);           
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);
  }
  return true;
         }
        });
public void onStart(Intent intent, int startId) {
          getPrefs();
}
     private void getPrefs() {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            ListPreference = prefs.getString("listPref", "nr1");
     }

这种方式非常有效。