Android デザイン パターン ガイドによると、ウィジェットのコンテンツとレイアウトは、ユーザーがサイズ変更操作を介して定義したサイズに動的に調整できます:ウィジェットのデザイン ガイド
設計ガイドに記載されている例:
しかし、これを達成する方法については、ドキュメントには何も表示されません。サイズ変更操作ごとにレイアウトを変更するにはどうすればよいですか? アプローチに関する任意のアイデアをいただければ幸いです。
Android デザイン パターン ガイドによると、ウィジェットのコンテンツとレイアウトは、ユーザーがサイズ変更操作を介して定義したサイズに動的に調整できます:ウィジェットのデザイン ガイド
設計ガイドに記載されている例:
しかし、これを達成する方法については、ドキュメントには何も表示されません。サイズ変更操作ごとにレイアウトを変更するにはどうすればよいですか? アプローチに関する任意のアイデアをいただければ幸いです。
A--C のおかげで、これは Jellybean 以上のデバイスで可能であり、実装が簡単です。onAppWidgetOptionsChanged
以下は、メソッドを使用したサンプルコードです
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onAppWidgetOptionsChanged(Context context,
AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
Log.d(DEBUG_TAG, "Changed dimensions");
// See the dimensions and
Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
// Get min width and height.
int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
int minHeight = options
.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
// Obtain appropriate widget and update it.
appWidgetManager.updateAppWidget(appWidgetId,
getRemoteViews(context, minWidth, minHeight));
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,
newOptions);
}
/**
* Determine appropriate view based on width provided.
*
* @param minWidth
* @param minHeight
* @return
*/
private RemoteViews getRemoteViews(Context context, int minWidth,
int minHeight) {
// First find out rows and columns based on width provided.
int rows = getCellsForSize(minHeight);
int columns = getCellsForSize(minWidth);
if (columns == 4) {
// Get 4 column widget remote view and return
} else {
// Get appropriate remote view.
return new RemoteViews(context.getPackageName(),
R.layout.quick_add_widget_3_1);
}
}
/**
* Returns number of cells needed for given size of the widget.
*
* @param size Widget size in dp.
* @return Size in number of cells.
*/
private static int getCellsForSize(int size) {
int n = 2;
while (70 * n - 30 < size) {
++n;
}
return n - 1;
}
@choletski @azendh
レイアウトを変更した後、一部のクリック イベントが呼び出されなくなりました
ビューでsetOnClickPendingIntentを作成し、それを返す関数を作成することで、この問題を解決しました。
たとえば、コードは次のようになります
private RemoteViews getConfiguredView (RemoteViews remoteViews, Context context){
Intent refreshIntent = new Intent(context, EarningsWidget.class);
refreshIntent.setAction(REFRESH_ACTION);
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 3, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refreshButton, toastPendingIntent);
return remoteViews;
}
そして、 「適切なリモートビューを取得する」場所で関数が呼び出されます。
return getConfiguredView(new RemoteViews(context.getPackageName(), R.layout.activity_widget), context);