动态调整窗口小部件的内容和布局的大小通过调整大小定义的用户。 Android的(Dynamica

2019-08-06 09:33发布

Android的设计模式指南说widget的内容和布局可以动态调整,以用户通过此调整操作所定义的大小: 设计指南部件

设计指南中提供的示例:

但我不认为在这个文档中任何关于如何做到这一点。 我们如何改变布局按照大小调整操作? 任何有关该方法的想法可以理解的。

Answer 1:

由于A - C,这是可能的和杰利贝恩上述设备和实现简单。 以下是使用的示例代码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;
 }


Answer 2:

@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);


Answer 3:

基于Gogu响应,并根据这个回答有关如何获取部件的尺寸,我创造了这个WidgetClassKotlin

class WidgetClass: AppWidgetProvider() {

    override fun onUpdate(context: Context?, appWidgetManager: AppWidgetManager?, appWidgetIds: IntArray?) {

        for (id in appWidgetIds!!) {
            //get widget options for later get widget dimensions
            val options = appWidgetManager?.getAppWidgetOptions(id)
            //get widget view based on widget size
            val view = getView(context, options)
            //update widget
            appWidgetManager!!.updateAppWidget(id, view)
        }

    }

    //listen for widget changes
    override fun onAppWidgetOptionsChanged(context: Context?, appWidgetManager: AppWidgetManager?,
                                           appWidgetId: Int, newOptions: Bundle?) {

        //update widget view based on new options
        appWidgetManager?.updateAppWidget(appWidgetId, getView(context, newOptions))

        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions)
    }

    private fun getView(context: Context?, options: Bundle?): RemoteViews {

        val minWidth: Int
        val minHeight: Int

        if (context!!.resources.configuration.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            || context.resources.configuration.orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {

            minWidth = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) ?: 0
            minHeight = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT) ?: 0

        } else {

            minWidth = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH) ?: 0
            minHeight = options?.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) ?: 0

        }

        //get widget view accordin widget size
        return if (minWidth >= 240)
            RemoteViews(context.packageName, R.layout.widget_large)
        else
            RemoteViews(context.packageName, R.layout.widget_small)
    }
}


文章来源: Dynamically adjusting widget's content and layout to the size the user defined through resize. Android