如何使android小部件布局响应

Bar*_*ick 8 android android-widget

我正在为我的Android应用程序构建一个小部件,它上面会有几个按钮.根据窗口小部件的大小,我希望窗口小部件上的按钮数量增长和缩小.这是4x1视图中目前的样子: 4x1小部件

当你将它缩小到3x1时: 3X1

反正有没有动态隐藏3x1视图中的第4个按钮,所以其他3个按钮不会被压扁?同样适用于2x1或1x1.对此的任何意见将不胜感激!

谢谢!

Jac*_*per 12

就像他们已经说过 您可以使用onAppWidgetOptionsChanged

所以我的主张是

1)制作不同的布局.带有3个和4个按钮.

2)根据窗口小部件列数更改布局.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {

    // 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 row or column 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);
    // Now you changing layout base on you column count 
    // In this code from 1 column to 4
    // you can make code for more columns on your own.
    switch (column) {
        case 1:  return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_1column);
        case 2:  return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_2column);
        case 3:  return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_3column);
        case 4:  return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_4column);
        default: return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_4column);
    }
}

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)


Jos*_*_GD 8

正如所建议的那样,除了onAppWidgetOptionsChanged在您的窗口小部件提供程序中实现之外别无他法- API级别<16用户不幸的是不会有此功能.

为了在窗口小部件缩小时隐藏按钮,您的窗口小部件的行为类似于Android设计站点中的示例,在模式" 窗口小部件 "(实际上是appwidgets)下给出.随着窗口小部件缩小,显示更少的按钮,随着窗口的增长,显示更多按钮,设置按钮的可见性.

在此输入图像描述

onAppWidgetOptionsChanged,使用newOptions Bundle给出的值,您必须检测宽度和高度的变化,并按照文档中的建议设置大小桶(来自同一个设计页面):

实际上,当用户调整窗口小部件的大小时,系统将以dp大小范围响应,您的窗口小部件可以在其中重绘自身.规划小部件调整大小策略跨"大小存储桶"而不是可变网格维度将为您提供最可靠的结果.

真正具有挑战性的是如何确定这些桶.newOptions Bundle的min_width,min_height,max_width和max_height值在设备,屏幕密度,发射器等之间存在很大差异.不幸的是,Android团队没有向我们展示如何执行此操作,并且onAppWidgetOptionsChanged网络中的代码示例很少,大多数他们很简单.


Fem*_*emi 0

您可以尝试监听onAppWidgetOptionsChanged回调:只要布局大小发生变化,该回调就会触发。但是,我(现在)确实没有办法确定您的小部件占用的实际屏幕空间量,因此没有好的方法可以根据大小更改按钮的可见性。