我为它创建了一个小部件 (HelloWidget.java)、一个活动 (MainActivity.java) 和一个列表首选项 (EditPreferences.java)。
XML 文件:
- Main.xml:这有小部件
- Config.xml:这有活动:按钮
- 首选项.xml:这有列表首选项
我创建了首选项以让用户更改小部件的背景图像。我在 drawable-hdpi 文件夹中有 4 个图像文件。默认背景设置为 android:background="@drawable/goldgreenbg"
在 MainActivity.java 中,如果用户单击 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));
}
不幸的是,这会导致更改活动,而不是小部件。所以现在我看到的是背景图像而不是活动中的按钮,而小部件没有改变。我试图把它放在 UpdateService.java 的 onCreate() 方法中,但它不能使用 setContentView()。有任何想法吗?
更新: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>
已解决:“If”部分应该在preferences.java 文件中,并在linearlayout 中使用此代码:
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);