本文翻译自developer.android.com/about/versi…

Android 12经过改善现有widget API,来优化用户和开发人员的领会。本文介绍Android 12中widget的新特性并介绍运用怎样才能够兼容Andrandroid是什么手机牌子oid 12。

Android 12新特性介绍——widget改善

保证你的widget与Android 12兼容

Android 12中一切的widget都是一致闪现成圆角的。所以任何Aapproachpp在Android 12上作业时,假定运用其widget Launcher会默许将其布景裁剪成圆角。
以下场景,widgandroid12et或许就会出现闪现不全或许异常:

  • widget旮旯中存在闪现内容,这或许会被体系裁剪android平板电脑价格掉,导致闪现不全
  • widget布景无法裁剪,假定运用了通明布景,空的视图或app装置下载许布局,或任何不容易裁剪的特别布景。体系或许无法正确辨认要运用的布景。

为了让App的widget正常闪现,首要咱们需求适配圆角。

正告: 圆角的android12规范或许会因设备而异,因为设备制造商(最大16dp)和第三方发射器都能够操控角半径的巨细。咱们建议改写窗口小部件android下载装置,以帮忙防止发生不满意的作用。

示例Android代码

关于新特性的示例代码 samplandroid手机e list wiAPPdget。

适配圆角

Android 12加入了体系参数来设置widget的圆角半径:

  • system_app_widget_APPbackgappearanceround_android体系radius: widget布景的圆角半径,android下载装置默许不会大于28dpapplication
  • system_app_widget_inner_radius:android手机widget内部view的圆角半径,这个默许比布景圆角小8dp来进行对齐

Android 12新特性介绍——widget改善

  1. widget的圆角。
  2. widget的内部视图圆角。appstore

正告: 圆角的规范或许会因设备而异,因为设备制造商(最大16dp)和第三方Launcher都能够操控角半径的巨细。

向后兼容

为了不影响Android 12之前的版别,而且向后兼容后续Android版别,请将圆角的资源装备放在valuesandroid体系-31的目录中

动态颜色适配

在Android 12中,widget能够运用当时设备的主题色来设置button、布景或许其他组件。这样有利于在Androidappreciateappstore界面间android下载切换结束滑润过渡。
在以下示例中,设备主题颜色为“棕褐色”,Android从而使配色和小部件布景习惯。您能够运用体系的默许主题(@android:style/Theme.DeviceDefault.DayNight)及其颜android下载色特征Android来结束app装置下载。一些常用的颜色特征是:

  • ?android:attr/colorAccentandroid平板电脑多少钱
  • ?androidapp装置下载:attr/colorBackandroid体系ground
  • ?android:attr/textColorPrimary 和 ?android:attr/textColorSecondary

Android 12新特性介绍——widget改善
亮色主题

Android 12新特性介绍——widget改善
暗色主题

向后兼容

同上适配圆角,为了前后兼容,请将Andrappointmentoid 12中的主题资源装备改动,一致放在values-31中。

简化定制widget流程

假定指定一个特别装备Activity,并装备appwidget-proivder中,当用户添加此widget之后,App Widget会马上建议这个Activity。

添加从头装备选项

android什么意思户长按widget之后,会出现一个从头装备的按钮,假定点击,能够进行更android下载改装备。

Android 12新特性介绍——widget改善

  1. 从头装备按钮

假定需求支撑此功用,需求在apandroid是什么手机牌子pwidget-provider的widgetFeatures特征中声明reconfigurable:

<appwidgeAndroidt-provider
...
android:configure="com.myapp.WidgetConfigActivity"
android:widgetFeatures="reconfigurable">
</appwidget-provider>

留神:该recAndroidonfigurable标志是在Android 9(APIandroid下载装置等级28)中引入的,但直到Android 12才在建议器中得到广泛支撑。

appointment用默许装备

运用默许装备假定期望widget运用默许装备,那么只需求在widgetFeatures声明configuration_optional和reconfigurable标志来越过装备进程。
例如时钟widgeandroid是什么手机牌子t能够绕过初始装备并默许闪现设备时区。

<appwidget-provider
...
andrandroid的drawable类oid:configurapproache="comapproach.myapp.WidgetConfigActivity"
android:widgetFeaturappstorees="reconfigurable|configuration_optional">
</appwidget-provider>

向后兼容

运用能够在前期版其他Android中运用configuration_optional和reconfigurable标志。可是,这些标志不会有任何作用,而且体系仍会建议装备Activity。

新的控件支撑

Android 12运用以下现有组件添加了对状况行为的新支撑:

  • CheckBox
  • Switch
  • RadioButton

结束办法:

// Check the view.
remoteView.setCompoundBandroid12uttonChecked(R.id.my_checkboappointmentx, true)
// CappointmenthAPPeck a radio group.appear
remoteViappearew.setRadioGroupChecked(R.id.my_radio_group, R.id.radio_button_2)
// Listen for check changes. The inteandroid体系nt will have an extra with the key
// EXTRA_CHECKED that specifies the currenapplicationt checked state of the view.
remoteView.setOnCheckedChangeResponse(
R.id.my_checkbox,
RemoteViews.RemoteResponse.fromPendingIntent(onCheckedChangePendingIntent)
)

向后兼容

供给两种不同的布局,一种针对作业Android 12或更高版别(res/layout-v31)的设备,另一种针对作业Android 12早年版其他设备(在默许res/layout文件夹中)。

运用API来调整widget巨细和布局

从Android 12开始,能够实行以下操作,结束更精密的规范特征和更活络的布局:APP

  1. 指定其窗口巨细捆绑
  2. 供给照顾式布局或许准确布局

指定widget巨细捆绑

AndAPProid 12添加了新的API,能够保证在屏幕规范不同的不同设备上更可靠地调整窗口小部件的规范。
除了android体系现有的minWiappledth, minHeight, minResizeWidth,和minResizeHeight 特征,运用下面的新appwidget-provider特征:

  • targetCellWidtapplicationh 和targetCellHeight:依据Launcher网格单元界说小部件的方针巨细。假定界说,则运用这些特征替代minWidth或minHeight。
  • maxResizeandroid体系Width 和maxResizeHeight:界说Launcher允许用户调整窗口小部件巨细的最大巨细。

以下XML描绘了怎样运用巨细设置特征:

<appwidget-provider
...
android:targetCellWidth="3"
anapprovedroid:app装置下载targetCellHeight=android平板电脑多少钱"2"
android:maappreciatexResizeWidth="250dpapplication"
android:maxResizeHeight="110dp">
</appwidget-provider>

供给照顾式布局
假定widget的布局需appear求随其巨细改动而改动,建议创建不同规范的布局。(假定不可行的话,别的一种适配便是在作业时依据widgandroid是什么手机牌子et的具体巨细供给准确布局)。
结束此功用能够更android是什么手机牌子好的兼容体系,体系没有必要在每次因为不同巨细闪现appearance的widget唤醒Appandroid是什么手机牌子
下面是示例代码怎android下载样供给一个布局列表:

override fun onUpdate(...) {
val smallView = ...
val tallView = ...
val wideVieappreciatew = ...
valandroid的drawable类 viewMapping: Map<SizeF, RemoteViews> = mapOf(
SizeF(100f, 100f) to smallView,
SizeF(100f, 200f) to tallView,
SizeFapple(200f, 100f) to wideView
)
val remoteViews = RemoteViews(viewMapping)
appWidgetManageandroid下载r.updateAppWidget(id, remoteViews)
}

供给准确布局

假定小部分的照application应式布局不可用,咱们能够改成与widget巨细适配的布局,也便是准确布局。通常在普通手机上有两种appear规范(横竖屏),在折叠屏上有四种布局。
为了结束此android体系方案,App需求实行以下进程:

  1. 重载 AppWidgetProvider#onAppWidgetOptionsChanged(…), 此办法会在规范改动的时分调用
  2. 调用 getAppWidgetManager#getAppWidgeappstoretOptions(…),此办法会回来apple一个包括规范的Bundle
  3. 从Bundle中获取 AppWidgeandroid平板电脑多少钱tManager.OPTION_APPWIDGET_SIZES

留神:Launcher供给规范列表,假定当时Launcher不支撑此字段,回来值或许是null

下面的代码示例演示怎样供给准确的布局:

// Create the RemoteViews for the given size.
private fun createRemoteVandroid手机iews(size: SizeF): RemoteViews { }
overrappearide fun onAppWidgetOptionsChanged(
capproachontext: Context,
manager: AppWidgetManagerapple,
id: Int,
newOptions: Bundle?
) {
super.onAppWidgetOptionsChanged(context, mandroid是什么手机牌子anager, id, newOptions)
// Get the new sizes.
val sizesAPP = newOptAndroidions?.getParcelableArrayList<SizeF>(
AppWidgetManager.OPTION_APandroid是什么手机牌子PWIDGET_SIZES
)
// Check that the list of sizes is provided by the launcher.
if (sizes.isNullOrEmpty()) {
returAPPn
}
/android是什么手机牌子/ Map the sizes to the desired RemoteViews
val remoteViews =android什么意思 RemoteViews(sizandroid手机es.associateWith(::createRandroid平板电脑多少钱emoteViews))
appWidgetManager.updateAppWidget(android平板电脑多少钱id, remoteViews)appreciate
}

向后兼容

在此之前,App能够经过 OPTION_APPWIDGET_Mandroid手机IN_WIDandroid平板电脑多少钱TH,OPTION_APPWIDGET_MIN_HEIGHT, OPTION_APPWIDGET_MAX_WIDTH,APP和 OPTION_APPWIDGET_MAX_HEandroid是什么手机牌子IGHT获取widget的规范,也能够经过预算得到,可是此逻辑android是什么手机牌子并不能掩盖一切的场景。依据AndroiAndroidd 12,建议运用照顾式或许准确式布局。

改善App Widget选择器体appointment

Android 12能够经过添加动态widget预览和描绘来改善App Widget选择器领会。

添加widget预览到选appstore择器中

在Android 12上,widget能够供给一个可缩放的预览在选择器中,而本来的XML只给widget供给巨细。在此之前,widget的预览是静态的图片资源,在某些状况下,或许存在预览和实践放在桌面上的作用不一样。
要结束这种缩放的预览,需求在appwidget-provider中运用previewLapp装置下载ayout特征:

<appwidget-provider
...
android:previewLayout="@layout/my_widget_preview">
</appwidget-provider>

抱负状况下,预览布局APP与实践widget的布局是千篇一律的。

向后兼容

假定在Aandroid平板电脑价格nroid 12之前的版别上,运用预览,请继续设置previewImage。别的假定修改了widget的外观,请一起更新预览。

为widget添加描绘

在Android 12中,能够为widget供给阐明。

Android 12新特性介绍——widget改善
在appwidget-prAPPovider中运用description即可为widget供给描绘:

<aappstoreppwidget-provider
..Android.
android:description="@string/my_widget_description">
</appwidget-provider>

留神:此处没有appstore字符捆绑,可是因为设备的空间影响,描绘尽量简洁明了。

向后兼容

在Android 12之前的版别,App能够运用widgetDesandroid什么意思cription,可是这个并不会在选择器appointment中闪现。

运用更滑润的过渡

在Android 12中,当用户从widget建议app时,Launcher可供给更滑润的过渡。
要启用此改善的过渡,请运用@android:id/backgroandroid下载und或 androandroid的drawable类id.android是什么手机牌子R.id.background标识您的布景元素android手机

// Top level layout of the widget.
<LinearLayout
...
android:id="@android:id/background">
</LinearLayout>

正告:为了防止播送溃散。Android 12中,运用依然经过播送或许服务建议,假定它经过widget的点击运用PendingIntent建议的。可是,新的动画作用并不会运用在播送或许服务approach建议,因为用户领会很差

向后兼容android是什么手机牌子

App能够运用@android:id/background在以往appreciate的版别中approach,可是没有任何作用。

运用简化的RemoteViewsandroid平板电脑多少钱集结

Androandroid手机id 12供给了appointment setRemoteAdapter(iandroid下载装置nt viewId,RemoteViews.RemoteCollectioapproachnItems items) 办法,这个能够让app填充ListViewapp装置下载的时分直接经过结合就能够结束。在早年,当运用ListView,咱们需求先结束界说一个RemappearanceoteViewsService,来获取RemoteViewsFactory。
假定集结中存在多个类型的布局,咱们能够运用setViewTypeCount指定集结能够包括最大android什么意思唯一布局数。
这里是示例怎样结束简化RemoteViappstoreeapp装置下载ws集结:

remapproveoteView.setRemoteAdapter(
R.id.list_view,android平板电脑价格
RemoteViews.RemoteCollectionItems.Builder()
.addItem(/* id= */ ID_1, RemoteViews(...))
.addItem(appear/* id= */ ID_2, RemoteViews(...))appointment
...
.sappearanceetViewTyAPPpeCount(MAX_NUM_DIFFERENT_REMOTE_VIEWS_LAYOUTS)
.build()
)

在作业时修改RemoteViews

Android 12中添加了几种在作业时修改RemotesViews特征的办法。详细的办法能够查阅RemoteViews API。
下面是示例新增的几个办法android手机

// Set the colors of a progress bar at runtime.
remoteView.sandroid是什么手机牌子etColorStateList(R.id.progress, "setandroid什么意思ProgressTintList", createProgressColorStateList())
// Specify exact sizes for margins.
remoteView.applicationsetViewLayoutMargin(R.id.text,app装置下载 RemoteViews.MARGIN_END, 8f, TypedValue.COMPLEX_UNIT_DP)