1. 程式人生 > 程式設計 >Android視窗小部件基礎編寫程式碼例項

Android視窗小部件基礎編寫程式碼例項

實現視窗小部件,訪問手機儲存卡指定目錄中的圖片檔案,然後隨機選擇一張在視窗的小部件中顯示。圖片路徑使用List儲存,適合初級Android學習者參考。本系統無服務,不能保證程序長存。

新建一個空的佈局專案,然後新建一個Widget,如圖所示:

在新建的xml和java實現類中進行編寫即可。

Android視窗小部件基礎編寫程式碼例項

picture_widget.xml檔案如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="@dimen/widget_margin">

  <ImageView
    android:id="@+id/picture_widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/zhizhuxia" />
  <TextView
    android:id="@+id/change_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="10dp"
    android:gravity="right|bottom"
    android:layout_marginRight="2dp"
    android:layout_marginBottom="2dp"/>
</RelativeLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。