Android widget你好(hello widget)

還在尋找無痛植入Android Widget的方法嗎?
最無痛
最容易
最輕鬆
Android Widget 學習方法就在這
只要7個簡單的步驟就可完成

1) 建立新Project

2) 修改 AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.dendroidZ.widget"  
    android:versionCode="1"  
    android:versionName="1.0">  
   <uses-sdk android:minSdkVersion="3" />  
   <application android:icon="@drawable/icon" android:label="@string/app_name">  
           <!-- widget 的更新 -->  
           <receiver android:name=".HelloWidget" android:label="@string/app_name">  
                <intent-filter>  
                     <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
                </intent-filter>  
                <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />  
           </receiver>  
   </application>  
 </manifest>  

3) 建立一個 widget.xml 檔

4) 修改 widget.xml
res/xml/widget.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <appwidget-provider  
   xmlns:android="http://schemas.android.com/apk/res/android"  
   android:minWidth="80dip"  
   android:minHeight="60dip"  
   android:updatePeriodMillis="3000"  
   android:initialLayout="@layout/main">  
 </appwidget-provider>  

5) 修改 main.xml
res/layout/main.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      android:orientation="vertical"  
      android:layout_width="fill_parent"  
      android:layout_height="fill_parent"       
      android:background="#E3E4FA">  
      <TextView 
           android:layout_width="fill_parent"  
           android:layout_height="fill_parent"  
           android:text="Widget 你好"  
           android:gravity="center"  
           android:textColor="#000000" />  
 </LinearLayout>  

6) 建立widget class
/src/package/HelloWidget.java
 import android.appwidget.AppWidgetProvider;  
 public class HelloWidget extends AppWidgetProvider{  
 }  

7) 測試結果





1 則留言: