我正在尝试将小部件添加到主屏幕,以允许用户查看某些数据库信息。不幸的是,似乎我无法让小部件注册我的Android系统。
这是清单文件:
<reciever android:name="com.Money.EasyMoney.FreeCashWidget"
android:label="Free Cash Widget 4x1"
android:icon="@drawable/ic_launcher">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<meta-data android:name="android.appwidget.AppWidgetProvider"
android:resource="@xml/freecashwidget"/>
</intent-filter>
</reciever>
</application>
这是res / xml / freecashwidget.xml中的widget-provider
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:minResizeHeight="72dp"
android:minResizeWidth="294dp"
android:updatePeriodMillis="0"
android:previewImage="@drawable/ic_launcher"
android:initialLayout="@layout/freecashwidget"
android:resizeMode="none">
</appwidget-provider>
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="294dp"
android:layout_height="72dp"
android:padding="@dimen/widget_margin">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:clickable="false"
android:background="@drawable/rectangle">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Free Cash:"
android:id="@+id/textWidgetFreeCash"
android:textSize="30sp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonRecordExpense"
android:background="@drawable/ic_action_spend"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</RelativeLayout>
这是接收者:
public class FreeCashWidget extends AppWidgetProvider
{
public void onUpdate(Context context, AppWidgetManager AppManager, int[] widgets)
{
super.onUpdate(context, AppManager, widgets);
final int n = widgets.length;
for(int i = 0; i < n; i++)
{
int widgetid = widgets[i];
//create an intent to launch the specified activity
Intent intent = new Intent(context, main.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
//get the layout and attach an onclicklistener for the button
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.freecashwidget);
view.setOnClickPendingIntent(R.id.buttonRecordExpense, pIntent);
//tell the app widget manager to perform an update ont eh current app widget
AppManager.updateAppWidget(widgetid, view);
}
}
}
它只是一个带有按钮的文本框,该按钮应该是4x1并且直接添加到我的应用程序中。我尝试重启设备。我很难过这个。
编辑:
一旦我运行它,它应该正确注册小部件吗?