我已经在stackoverflow上看过关于这个的几个问题,没有一个答案解决了我的问题.
我正在尝试添加一个主屏幕小部件到我的应用程序,我的小部件不会出现在Android的小部件列表中.
我已经尝试重新启动设备,重新安装应用程序,更改配置文件中的参数,确保SD卡上没有安装应用程序.没有任何工作.
这是我现在的代码:
AndroidManifest.xml的应用程式标签内
<receiver android:name=".GulpWidgetProvider" android:icon="@mipmap/ic_launcher" android:label="Gulp"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <Meta-data android:name="android.appwidget.provider" android:resource="@xml/gulp_widget_info" /> </receiver>
里面的/res/xml/gulp_widget_info.xml:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/layout_widget" android:minHeight="150dip" android:minWidth="400dip" android:updatePeriodMillis="86400000" android:widgetCategory="home_screen" android:resizeMode="none" android:minResizeHeight="150dip"/>
里面/res/layout/layout_widget.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="150dip" android:background="@drawable/gradient" android:gravity="center_horizontal|top"> <RelativeLayout android:layout_width="170dip" android:layout_height="150dip" android:gravity="center"> <ProgressBar android:id="@+id/widget_consumption_progress_bar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="150dip" android:layout_height="150dip" android:layout_centerInParent="true" android:indeterminate="false" android:max="100" android:progress="0" android:progressDrawable="@drawable/progressbar" android:secondaryProgress="100" /> <ProgressBar android:id="@+id/widget_time_progress_bar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="150dip" android:layout_height="150dip" android:layout_centerInParent="true" android:indeterminate="false" android:max="100" android:progress="0" android:progressDrawable="@drawable/progressbar2" android:secondaryProgress="100" /> <TextView android:id="@+id/widget_water_consumed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:fontFamily="sans-serif-light" android:gravity="center" android:maxLines="1" android:textColor="@color/white" android:textSize="30sp" /> <TextView android:id="@+id/widget_unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/water_consumed" android:layout_centerInParent="true" android:fontFamily="sans-serif-light" android:gravity="center" android:maxLines="1" android:text="@string/milliliters_abbrev" android:textColor="@color/white" android:textSize="12sp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip"> <TextView android:id="@+id/widget_add" style="@style/GulpButton" android:layout_width="60dip" android:layout_height="60dip" android:background="@drawable/whitecirclebutton" android:gravity="center" android:text="@string/add_abbrev" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/widget_goal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="sans-serif-light" android:textColor="@color/white" android:textSize="22sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="sans-serif-light" android:text="@string/target" android:textColor="@color/white_semi_translucent" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/widget_remaining" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="sans-serif-light" android:textColor="@color/white" android:textSize="22sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="sans-serif-light" android:text="@string/remaining" android:textColor="@color/white_semi_translucent" android:textSize="14sp" /> </LinearLayout> </LinearLayout> </LinearLayout>
里面/java/info/andrewdahm/gulp/GulpWidgetProvider.java:
public class GulpWidgetProvider extends AppWidgetProvider { @Override 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,Main.class); PendingIntent pendingIntent = PendingIntent.getActivity(context,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.layout_widget); views.setOnClickPendingIntent(R.id.widget_add,pendingIntent); // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId,views); } } }
任何想法为什么我的窗口小部件不会出现在窗口小部件列表中?
解决方法
这是关于在gulp_widget_info.xml中为android设置的值的问题:minHeight和android:minWidth.
尝试设置较小的值,小部件应显示在启动器的小部件列表中:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/layout_widget" android:minHeight="40dip" android:minWidth="40dip" android:updatePeriodMillis="86400000" android:widgetCategory="home_screen" android:resizeMode="none" />
我在Google手机启动器中测试了这项设置,并显示为1×1
计算大小的规则是70×n-30,其中n是所需的单元格数.您可以找到有关如何确定小部件here的大小的更多信息.