I have a (hopefully) a relatively simple qu开发者_如何学编程estion. How do I tell Android which layout to use for portrait and which layout to use for landscape orientation on my AppWidget?
Thanks in advance!
First, bear in mind that some home screens (e.g., Nexus One) do not change orientation.
The standard approach for having different layouts per orientation is to put the portrait version in res/layout/
, and the landscape version in res/layout-land/
, both under the same name (e.g., appwidget.xml
). Then, just refer to it by name (e.g., R.layout.appwidget
), and Android will choose the file based on the orientation. This definitely works for activities, so I would assume it works for app widgets, but I don't think I have actually tried it.
To change the width and height of the app widget, define two different android:layout_height
and android:layout_width
sizes for your layout (in the res/layout-port
and res/layout-land
). Then define the larger width and height in your appwidgetinfo.xml
. That way, the app widget changes size when the orientation is changed and not just when you add the widget to the homescreen.
If you are using RemoteViews to dynamically update your widget you can supply two separate RemoteViews to the updateAppWidget function call. For example:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews rvLandscape=new Remoteviews(context,R.layout.widget_landscape);
rvLandscape.addView(...
:
RemoteViews rvPortrait =new Remoteviews(context, R.layout.widget_portrait);
rvPortrait.addView(...
:
appWidgetManager.updateAppWidget(widgetId, new RemoteViews(rvLandscape, rvPortrait));
}
精彩评论