rek*_*ire 13 android homescreen android-widget
我只是想知道我当前的小部件有多大.我找到了很多问题来设置最小尺寸,但我不想设置它.相反,我想显示那些只适合小部件的信息.
如果小部件太小我需要隐藏一些东西,但我需要知道它的大小.我阅读了一些类的源代码AppWidgetProvider,甚至是文档.总有一些关于最小或最大尺寸的参考,但绝不是当前尺寸.
请指出正确的资源.
Jür*_*ann 10
不幸的是,仍然没有api从AppWidget-Host获取此信息.
您只能获得有关调整大小事件(Android 4.1+)的当前大小信息,并且仅获得支持此功能的启动器(例如Sony XPeria Launcher不支持,以及某些第三方启动器).
我按照这个stackoverflow线程和Android开发者文档:
这是我的代码,用于确定小部件大小,取自图片日历2014(Play商店):
/* Get Device and Widget orientation.
This is done by adding a boolean value to
a port resource directory like values-port/bools.xml */
mIsPortraitOrientation = getResources().getBoolean(R.bool.isPort);
// Get min dimensions from provider info
AppWidgetProviderInfo providerInfo = AppWidgetManager.getInstance(
getApplicationContext()).getAppWidgetInfo(appWidgetId);
// Since min and max is usually the same, just take min
mWidgetLandWidth = providerInfo.minWidth;
mWidgetPortHeight = providerInfo.minHeight;
mWidgetPortWidth = providerInfo.minWidth;
mWidgetLandHeight = providerInfo.minHeight;
// Get current dimensions (in DIP, scaled by DisplayMetrics) of this
// Widget, if API Level allows to
mAppWidgetOptions = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mAppWidgetOptions = getAppWidgetoptions(mAppWidgetManager,
appWidgetId);
if (mAppWidgetOptions != null
&& mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) > 0) {
if (D.DEBUG_SERVICE)
Log.d(TAG,
"appWidgetOptions not null, getting widget sizes...");
// Reduce width by a margin of 8dp (automatically added by
// Android, can vary with third party launchers)
/* Actually Min and Max is a bit irritating,
because it depends on the homescreen orientation
whether Min or Max should be used: */
mWidgetPortWidth = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
mWidgetLandWidth = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
mWidgetLandHeight = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
mWidgetPortHeight = mAppWidgetOptions
.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
// Get the value of OPTION_APPWIDGET_HOST_CATEGORY
int category = mAppWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
// If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen
// widget (dumped with Android-L preview :-( ).
mIsKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
} else {
if (D.DEBUG_SERVICE)
Log.d(TAG,
"No AppWidgetOptions for this widget, using minimal dimensions from provider info!");
// For some reason I had to set this again here, may be obsolete
mWidgetLandWidth = providerInfo.minWidth;
mWidgetPortHeight = providerInfo.minHeight;
mWidgetPortWidth = providerInfo.minWidth;
mWidgetLandHeight = providerInfo.minHeight;
}
if (D.DEBUG_SERVICE)
Log.d(TAG, "Dimensions of the Widget in DIP: portWidth = "
+ mWidgetPortWidth + ", landWidth = " + mWidgetLandWidth
+ "; landHeight = " + mWidgetLandHeight
+ ", portHeight = " + mWidgetPortHeight);
// If device is in port oriantation, use port sizes
mWidgetWidthPerOrientation = mWidgetPortWidth;
mWidgetHeightPerOrientation = mWidgetPortHeight;
if (!mIsPortraitOrientation)
{
// Not Portrait, so use landscape sizes
mWidgetWidthPerOrientation = mWidgetLandWidth;
mWidgetHeightPerOrientation = mWidgetLandHeight;
}
Run Code Online (Sandbox Code Playgroud)
在Android 4.1+上,您现在可以使用当前设备方向的DIP中的Widget的当前大小.要知道您的窗口小部件的宽度和高度有多少主屏幕网格单元,您必须将其除以单元格大小.默认值为74dip,但这可能因使用不同的设备或启动器而异.允许更改网格的自定义启动器对于Widget开发人员来说是一件痛苦的事.
在Android <4.1上,无法获取当前的小部件大小.因此,最好将您的API级别设置为JellyBean以避免麻烦.现在应该没问题......两年前,当我开始使用我的Widget应用程序时,这不是一个选项.
获取有关方向更改的通知以重绘窗口小部件是另一个主题.
重要提示:如果您允许用户拥有多个小部件,则必须根据其ID对每个人进行此计算!
这是以像素为单位获取小部件大小的类助手
请注意,您应该使用应用程序上下文,否则小部件旋转时方向将不正确
class WidgetSizeProvider(
private val context: Context // Do not pass Application context
) {
private val appWidgetManager = AppWidgetManager.getInstance(context)
fun getWidgetsSize(widgetId: Int): Pair<Int, Int> {
val isPortrait = context.resources.configuration.orientation == ORIENTATION_PORTRAIT
val width = getWidgetWidth(isPortrait, widgetId)
val height = getWidgetHeight(isPortrait, widgetId)
val widthInPx = context.dip(width)
val heightInPx = context.dip(height)
return widthInPx to heightInPx
}
private fun getWidgetWidth(isPortrait: Boolean, widgetId: Int): Int =
if (isPortrait) {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
} else {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
}
private fun getWidgetHeight(isPortrait: Boolean, widgetId: Int): Int =
if (isPortrait) {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
} else {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
}
private fun getWidgetSizeInDp(widgetId: Int, key: String): Int =
appWidgetManager.getAppWidgetOptions(widgetId).getInt(key, 0)
private fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7275 次 |
| 最近记录: |