简体   繁体   English

Android设置小部件背景

[英]Android set widget background

In my drawable-hdpi folder i have 4 image files (.png) to serve as the background of awidget. 在我的drawable-hdpi文件夹中,我有4个图像文件(.png)作为awidget的背景。 By default android:background="@drawable/goldgreenbg" is set for the LinearLayout. 默认情况下,为LinearLayout设置了android:background =“ @ drawable / goldgreenbg”。 I created a preferences screen to let the user change the background. 我创建了一个首选项屏幕,以允许用户更改背景。 How to do that? 怎么做? I would like to use this code for it: 我想使用此代码:

      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();
          }

Update: Where shall i put this code to? 更新:我应该将此代码放在哪里? MainActivity.java: for the activity UpdateService.java: for the widget EditPreferences.java: for the preferences Main.xml includes the listview and widgetlayout is id of it. 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));
  }

Assuming you allready have the LinearLayout on your screen (using setContentView ), you can change the background quite easily like so: 假设您已经在屏幕上使用LinearLayout (使用setContentView ),则可以很容易地更改背景,如下所示:

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

(and get that layout using findViewById() ofcourse ) (并使用当然的findViewById()获得该布局)

I found the solution. 我找到了解决方案。

EditPreferences.java: 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");
     }

This way it is working perfectly. 这样,它运行完美。

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

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