公司项目有个需求,需要在发送Notification的时候动态加载服务器的图片,经研究实现。大概思路如下
自定义Notification的布局文件,这样能够很方便设置View的属性。
首先加载网络图片,使用BitmapFactory.decodeStream解析出Bitmap,然后设置到自定义布局文件中的ImageView上。
自定义Notification布局如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="45dip"
- android:layout_height="45dip"
- android:layout_alignParentLeft="true"
- android:layout_marginBottom="8.0dip"
- android:layout_marginLeft="8.0dip"
- android:layout_marginRight="10dp"
- android:layout_marginTop="8.0dip" />
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8.0dip"
- android:layout_toRightOf="@id/image"
- android:textSize="16.0dip" />