标签:
通知有几种,吐司Toast是其中一种。以下介绍系统默认的通知,这个有时不能满足项目需求,需要自定义的Toast
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.notification.MainActivity" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="36dp" android:text="发送普通通知" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="35dp" android:text="自定义通知" /> </RelativeLayout>自定义Toast的布局custom_dialog.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:background="#888888" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="70dp" android:layout_height="70dp" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textColor="#892288" android:textSize="20dp" /> </LinearLayout>重点业务逻辑
package com.example.notification; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private Button button1; private Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast toast = Toast.makeText(MainActivity.this, "普通通知", Toast.LENGTH_SHORT); /*设置通知的位置,默认在屏幕的下方 * 第一个参数:一个常量,通知的位置。 * 第二个参数:x轴偏移量 * 第三个参数:y轴偏移量 */ toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast toast = new Toast(MainActivity.this); View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom_dialog, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageView1); imageView.setImageResource(R.drawable.notify); TextView textView = (TextView) view.findViewById(R.id.textView1); textView.setText("今天下午要开会"); toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 20); toast.setView(view); toast.show(); } }); } }工程的ImageView用到的图片资源notify.jpg要放到drawable文件夹下。说点题外话,工程用到的图片资源除了9-patch的,最好用png的,因为jpg格式的压缩后图片像素可能有所损失
结果
标签:
原文地址:http://blog.csdn.net/lindonglian/article/details/43340219