Android listpreference and widget background

ε祈祈猫儿з 提交于 2020-01-07 04:10:47

问题


I created a widget (HelloWidget.java), an activity for it (MainActivity.java) and a listpreference (EditPreferences.java).

XML files:

  • Main.xml: this has the widget
  • Config.xml: this has the activity: buttons
  • preferences.xml: this has the listpreference

I created the preferences to let the user change the background image of the widget. I have 4 image files for this in the drawable-hdpi folder. Default background is set like android:background="@drawable/goldgreenbg"

In MainActivity.java i have this code to set the background image if user clicks the first or the second element of the listpreference:

preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String listpref = preferences.getString("listPref", "n/a");              

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

Unfortunately this results in changing the activity, not the widget. So now i see the background image instead of the buttons in the activity while the widget is unchanged. I tried to put this in the onCreate() method of UpdateService.java but it doesn't enable setContentView() to use. Any ideas?

Update: main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/goldgreenbg"
    android:id="@+id/widgetlayout">
<TextView android:id="@+id/widget_textview"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textColor="#0B3B0B"
    android:textSize="11sp"/>

<TextView android:id="@+id/widget_textview2"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textSize="12sp"
    android:textColor="@android:color/white"/>
<TextView android:id="@+id/widget_textview3"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textSize="9sp"
    android:textColor="#0B3B0B"/>
</LinearLayout>

Solved: The "If" part should be in the preferences.java file and insted of linearlayout use this code:

 RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(215, 215, 215));
              updateViews.setTextColor(R.id.widget_textview2, Color.WHITE);
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(155, 155, 155));
              updateViews.setImageViewBitmap(R.id.ImageView01, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.blackbg)).getBitmap());
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);

回答1:


Your problem is right here:

LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout);
ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg));

Your setting the whole layout (or at least whatever is inside LinearLayout). You should be using the widget instead.



来源:https://stackoverflow.com/questions/4912858/android-listpreference-and-widget-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!