ウィジェット(HelloWidget.java)、そのアクティビティ(MainActivity.java)、およびリスト設定(EditPreferences.java)を作成しました。
XMLファイル:
- Main.xml:これにはウィジェットがあります
- Config.xml:これにはアクティビティがあります:ボタン
- Preferences.xml:これにはlistpreferenceがあります
ユーザーがウィジェットの背景画像を変更できるようにするための設定を作成しました。drawable-hdpiフォルダーにこのための4つの画像ファイルがあります。デフォルトの背景はandroid:background = "@ drawable/goldgreenbg"のように設定されています
MainActivity.javaには、ユーザーがリスト設定の最初または2番目の要素をクリックした場合に背景画像を設定するための次のコードがあります。
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);