Android 无法加载具有浮动操作按钮的小部件
当我将一个小部件放在主屏幕上时,它显示的不是其各自的布局,而是内容有问题的小部件加载小部件 当我们把普通按钮放进去的时候,同样的布局也很好 小部件布局Android 无法加载具有浮动操作按钮的小部件,android,android-widget,floating-action-button,Android,Android Widget,Floating Action Button,当我将一个小部件放在主屏幕上时,它显示的不是其各自的布局,而是内容有问题的小部件加载小部件 当我们把普通按钮放进去的时候,同样的布局也很好 小部件布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" andr
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:transitionGroup="true"
tools:context=".MainActivity">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:text="Widget" />
</RelativeLayout>
AppWidgetProviderClass
public class OpenMapAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, OpenMap.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
公共类OpenMapAppWidgetProvider扩展了AppWidgetProvider{
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
final int N=appWidgetIds.length;
//对属于此提供商的每个应用程序小部件执行此循环过程
对于(int i=0;i
应用程序小部件的文档声明RemoteViews对象(以及相应的应用程序小部件)可以支持布局列表和
LinearLayout
是其中之一,com.github.clans.fab.FloatingActionButton
也是doc支持的android.widget.ImageButton
的子类,那么为什么会出现此问题呢您不能在RemoteView中使用自定义视图(包括现有视图的扩展),因此您必须坚持支持的基本视图类型。应用程序小部件表示支持以下布局类:框架布局
,线性布局
,相对布局
,网格布局
,以及以下小部件类:模拟时钟
,按钮
,计时器
,图像按钮
,ImageView
,ProgressBar
,TextView
,ViewFlipper
,ListView
,GridView
,AdapterViewWFLipper
最后一行说“不支持这些类的后代。”com.github.clans.fab.FloatingActionButton是一个子类candroid.widget.ImageButton的类
。这些子类不支持
public class OpenMapAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, OpenMap.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}