我知道這是一個老問題,但有確實更簡單的方法來做到這一點比更新委託給一個IntentService。 處理方向更改的方法是監聽應用程序中的配置更改並向控件廣播ACTION_APPWIDGET_UPDATE。
添加應用程序類,您的應用程序
public class MyApplication extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// create intent to update all instances of the widget
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidget.class);
// retrieve all appWidgetIds for the widget & put it into the Intent
AppWidgetManager appWidgetMgr = AppWidgetManager.getInstance(this);
ComponentName cm = new ComponentName(this, MyWidget.class);
int[] appWidgetIds = appWidgetMgr.getAppWidgetIds(cm);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
// update the widget
sendBroadcast(intent);
}
}
每當配置發生變化的方法將被調用,其中之一是一個方向變化。只有在方向改變時才能改進該方法,而不是系統語言的改變。 該方法基本上發送更新廣播到你的小部件的所有實例。
中定義爲MyApplication您的清單
<application
android:name="yourpackagename.MyApplication"
android:description="@string/app_name"
android:label="@string/app_name"
android:icon="@drawable/app_icon">
<!-- here go your Activity definitions -->
</application>
與-land和-port文件夾的解決方案的問題是,它往往需要你複製整個佈局文件,以使一些或在我的情況下,一個幾個人物變化。據說,這似乎是唯一的解決方案。 –
有時,您可以使用縱向和橫向不同的資源(整數,樣式等)自動控制此選項。你甚至可以在你的單一佈局中有兩個平行的視圖,這些視圖根據方向自動做出VISIBLE/GONE。在你的更新方法中,你可以填寫兩個部件,而且部件容器將根據方向使用正確的部件。 – user1139880