Launcher小部件使用自定义View (仅限Launcher内部的小部件)
桌面小部件使用自定义的View方法
1. AppWidgetProvider.java
修改代码AppWidgetProviderInfo.java
修改CLS_CUSTOM_WIDGET_PREFIX为Launcher内部小部件的包名前缀就可以。比如我的小部件在com.demo包里面。
public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
public static final String CLS_CUSTOM_WIDGET_PREFIX = "com.demo"; //"#custom-widget-";
...
}
2. 桌面小部件实现demo
2.1 com.demo.MyWidgetProvider
public class MyWidgetProvider extends AppWidgetProvider {
}
2.2 res/layout/my_widget_view.xml
<?xml version="1.0" encoding="utf-8"?>
<com.demo.MyWidgetView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
</LinearLayout>
</com.demo.MyWidgetView>
2.3 res/xm/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/my_widget_view"
android:minHeight="@dimen/widget_min_height"
android:minWidth="@dimen/widget_min_width"
android:previewImage="@drawable/ic_app_widget"
android:resizeMode="none"
android:updatePeriodMillis="86400000"></appwidget-provider>
2.4 AndroidManifest.xml
<application>
<receiver
android:name="com.demo.MyWidgetProvider"
android:label="@string/torch_widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/my_widget_info" />
</receiver>
</application>