自定义Notification

来源:互联网 发布:阿里妈妈淘宝联盟区 编辑:程序博客网 时间:2024/06/05 00:41
 可以通过Notification.Builder.setContent方法自定义Notification。setContent方法的原型如下:
public Builder setContent(RemoteViews views);
 RemoteViews 是一种视图容器,只是这种视图容器支持的视图有限,目前RemoteViews 只支持布局和控件: 布局:FrameLayout、LinearLayout、RelativeLayout 控件:AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView RemoteViews类构造方法的原型如下:
public RemoteViews(String packageName, int layout);
      packageName:包名      layout与RemoteViews对象关联的布局资源ID
public class NotificationActivity extends Activity {    private NotificationManager manager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);    }    /**     * 显示通知     * @param view     */    public void showNotification(View view){        //创建RemoteViews对象        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);        //设置布局中的TextView显示的文本        remoteViews.setTextViewText(R.id.tv001,"更新自定义文本内容");        //创建Builder对象        Notification.Builder builder = new Notification.Builder(this)                .setSmallIcon(R.drawable.person).setContent(remoteViews);        //显示自定义的Notification        manager.notify(1,builder.build());    }}
<?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="vertical"    android:gravity="center">    <TextView        android:id="@+id/tv001"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textColor="@android:color/black"        android:gravity="center"        android:textSize="20dp"        android:text="自定义内容"/>    <ImageView        android:id="@+id/iv001"        android:layout_width="40dp"        android:layout_height="40dp"        android:src="@drawable/person" /></LinearLayout>
0 0
原创粉丝点击