gpt4 book ai didi

android - 如何使android小部件布局响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:29 26 4
gpt4 key购买 nike

我正在为我的 Android 应用程序构建一个小部件,上面有几个按钮。根据小部件的大小,我希望小部件上的按钮数量增加和缩小。这是它当前在 4x1 View 中的样子: 4x1 widget

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

有没有办法在 3x1 View 中动态隐藏第 4 个按钮,这样其他 3 个按钮就不会被压扁?同样适用于 2x1 或 1x1。对此的任何意见将不胜感激!

谢谢!

最佳答案

就像他们已经说过的那样。您可以使用 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 (columns) {
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;
}

关于android - 如何使android小部件布局响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17138191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com