随着android的手机屏幕越来越大,为了丰富屏幕内容 app widget(窗口小组件)被越来越多的应用所使用。app widget 有什么好处呢?它可以在不启动应用程序的情况下,让用户在屏幕上有一块交互窗口和程序入口点。
这是我手机自带的天气预报app widget效果。
为了创建一个应用程序的widget我们需要创建三个组件:
(1)给widget创建一个布局资源。
res/layout/nview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开网页" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开照相机" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开电话本" /> </LinearLayout>