动态调整小部件的内容和布局到用户通过 resize 定义的大小。安卓

Posted

技术标签:

【中文标题】动态调整小部件的内容和布局到用户通过 resize 定义的大小。安卓【英文标题】:Dynamically adjusting widget's content and layout to the size the user defined through resize. Android 【发布时间】:2012-12-25 13:28:32 【问题描述】:

android 设计模式指南说小部件的内容和布局可以通过 resize 操作动态调整到用户定义的大小:Design guide for widgets

设计指南中提供的示例:

但我在文档中没有看到关于如何完成此操作的任何内容。我们如何根据调整大小操作更改布局?任何有关该方法的想法将不胜感激。

【问题讨论】:

【参考方案1】:

感谢 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;
 

【讨论】:

【参考方案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);

【讨论】:

【参考方案3】:

基于Gogu 响应并根据this answer 关于如何获取小部件大小,我在Kotlin 中创建了这个WidgetClass

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)
    

【讨论】:

以上是关于动态调整小部件的内容和布局到用户通过 resize 定义的大小。安卓的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 - 动态添加小部件到布局

“分享此链接”HTML 小部件

如何使用“自动调整大小”避免 Android 中的小部件重叠。

Qt 在另一个小部件中调整小部件的大小

将自定义组件小部件动态添加到 Android 中的布局中

PyQt - 手动调整菜单小部件的大小不会调整其内容的大小