`
咖啡动力
  • 浏览: 434863 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

自定义Android,toast,以及多线程toast

 
阅读更多
Toast用于向用户显示一些帮助/提示。定义一个属于你自己的Toast。
举例5种效果

1.默认效果



代码

Toast.makeText(getApplicationContext(), "默认Toast样式",
     Toast.LENGTH_SHORT).show();



2.自定义显示位置效果



代码

toast = Toast.makeText(getApplicationContext(),
     "自定义位置Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   toast.show();



3.带图片效果





代码

toast = Toast.makeText(getApplicationContext(),
     "带图片的Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   LinearLayout toastView = (LinearLayout) toast.getView();
   ImageView imageCodeProject = new ImageView(getApplicationContext());
   imageCodeProject.setImageResource(R.drawable.icon);
   toastView.addView(imageCodeProject, 0);
   toast.show();



4.完全自定义效果



代码

LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom,
     (ViewGroup) findViewById(R.id.llToast));
   ImageView image = (ImageView) layout
     .findViewById(R.id.tvImageToast);
   image.setImageResource(R.drawable.icon);
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");
   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
   text.setText("完全自定义Toast");
   toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();



5.其他线程



代码

new Thread(new Runnable() {
    public void run() {
     showToast();
    }
   }).start();





完整代码

1.Main,java

package com.wjq.toast;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  findViewById(R.id.btnSimpleToast).setOnClickListener(this);
  findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
    this);
  findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
  findViewById(R.id.btnCustomToast).setOnClickListener(this);
  findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);

}

public void showToast() {
  handler.post(new Runnable() {

   @Override
   public void run() {
    Toast.makeText(getApplicationContext(), "我来自其他线程!",
      Toast.LENGTH_SHORT).show();

   }
  });
}

@Override
public void onClick(View v) {
  Toast toast = null;
  switch (v.getId()) {
  case R.id.btnSimpleToast:
   Toast.makeText(getApplicationContext(), "默认Toast样式",
     Toast.LENGTH_SHORT).show();
   break;
  case R.id.btnSimpleToastWithCustomPosition:
   toast = Toast.makeText(getApplicationContext(),
     "自定义位置Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   toast.show();
   break;
  case R.id.btnSimpleToastWithImage:
   toast = Toast.makeText(getApplicationContext(),
     "带图片的Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   LinearLayout toastView = (LinearLayout) toast.getView();
   ImageView imageCodeProject = new ImageView(getApplicationContext());
   imageCodeProject.setImageResource(R.drawable.icon);
   toastView.addView(imageCodeProject, 0);
   toast.show();
   break;
  case R.id.btnCustomToast:
   LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom,
     (ViewGroup) findViewById(R.id.llToast));
   ImageView image = (ImageView) layout
     .findViewById(R.id.tvImageToast);
   image.setImageResource(R.drawable.icon);
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");
   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
   text.setText("完全自定义Toast");
   toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
   break;
  case R.id.btnRunToastFromOtherThread:
   new Thread(new Runnable() {
    public void run() {
     showToast();
    }
   }).start();
   break;

  }

}
}



2.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:padding="5dip" android:gravity="center">
<Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
  android:text="默认"></Button>
<Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="自定义显示位置"
  android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
<Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
  android:text="带图片"></Button>
<Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="完全自定义"
  android:id="@+id/btnCustomToast"></Button>
<Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="其他线程"
  android:id="@+id/btnRunToastFromOtherThread"></Button>

</LinearLayout>



3.custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="vertical"
android:id="@+id/llToast" >
<TextView
  android:layout_height="wrap_content"
  android:layout_margin="1dip"
  android:textColor="#ffffffff"
  android:layout_width="fill_parent"
  android:gravity="center"
  android:background="#bb000000"
  android:id="@+id/tvTitleToast" />
<LinearLayout
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:id="@+id/llToastContent"
  android:layout_marginLeft="1dip"
  android:layout_marginRight="1dip"
  android:layout_marginBottom="1dip"
  android:layout_width="wrap_content"
  android:padding="15dip"
  android:background="#44000000" >
  <ImageView
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_width="wrap_content"
   android:id="@+id/tvImageToast" />
  <TextView
   android:layout_height="wrap_content"
   android:paddingRight="10dip"
   android:paddingLeft="10dip"
   android:layout_width="wrap_content"
   android:gravity="center"
   android:textColor="#ff000000"
   android:id="@+id/tvTextToast" />
</LinearLayout>
</LinearLayout>

分享到:
评论
1 楼 liyan12167 2011-08-12  
其中来自其他线程的toast报错

相关推荐

    Qt仿Android的toast控件效果

    使用Qt Widget Application技术实现桌面操作系统下类似Android的toast控件效果。Demo中实现为多线程的单例模式。

    Android代码-ToastUtils

    吐司工具类 &gt; 博客地址:只需体验三分钟,你就会跟我一样,爱上这款Toast &gt; 已投入公司项目多时,没有任何毛病,可胜任任何需求,点击此处下载Demo ...功能强大:不分主次线程都可以弹出Toast,自动区

    android开发资料大全

    在Android中实现多线程断点下载 Android提高篇内容整理 android移动开发案例精选 Android通过画线实现button效果 Android如何防止apk程序被反编译 Android 之 AIDL 和远程 Service 调用 Android 相对布局技巧 ...

    Android开发案例驱动教程 配套代码

    8.1 多线程案例--计时器 155 8.2 线程概念 156 8.2.1 进程概念 156 8.2.2 线程概念 156 8.3 Java中的线程 157 8.3.1 Java中的实现线程体方式1 157 8.3.2 Java中的实现线程体方式2 160 8.3.3 Java中的实现线程...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    2.2 Android中的多进程模式 36 2.2.1 开启多进程模式 36 2.2.2 多进程模式的运行机制 39 2.3 IPC基础概念介绍 42 2.3.1 Serializable接口 42 2.3.2 Parcelable接口 45 2.3.3 Binder 47 2.4 Android中的IPC...

    Android开发艺术探索

    2.2 Android中的多进程模式 / 36 2.2.1 开启多进程模式 / 36 2.2.2 多进程模式的运行机制 / 39 2.3 IPC基础概念介绍 / 42 2.3.1 Serializable接口 / 42 2.3.2 Parcelable接口 / 45 2.3.3 Binder / 47 ...

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--利用FinalHttp实现多线程断点续传 |--加密之MD5 |--动画Animation详解 |--动画之view左右抖动 |--动画之移动动画 |--动画之组合动画 |--动画之缩放动画ScaleAnimation |--反序列化对象 |--发送短信 读天气 调音量...

    精通ANDROID 3(中文版)1/2

    11.1.5 解决多线程问题  11.1.6 有趣的超时  11.1.7 使用HttpURLConnection  11.1.8 使用AndroidHttpClient  11.1.9 使用后台线程(AsyncTask)  11.1.10 使用AsyncTask处理配置更改  11.1.11 使用...

    文件互传(自家用),含打包好的apk与jar,含该项目的AndroidStudio与Eclipse源码

    Andriod Studio(java):Jackson的应用、Handler多线程间通信、自定义Adapter、Fontawesome在Android的应用、sun.misc.BASE64Encoder在Andriod的应用、 共有:Socket的通信、Base64加密与解密、MD5码验证、分包思想...

    基于android手机上的软件管理器源代码

    本项目涉及的知识有:ListView、GridView适配器的复写及ListView和GridView视图的切换,多线程+handler+进度条对话框的使用,自定义Toast的用法,四种动画效果及Layout里面子View的动画效果的使用,具有返回值的...

    android开发艺术探索高清完整版PDF

    16 1.2.2 Activity的Flags / 27 1.3 Intent Filter的匹配规则 / 28 第2章 IPC机制 / 35 2.1 Android IPC简介 / 35 2.2 Android中的多进程模式 / 36 2.2.1 开启多进程模式 / 36 2.2.2 多进程模式的运行机制 / ...

    android开发秘籍

    第3 章 线程、服务、receiver 以及alert 对话框 40 3.1 线程 40 3.1.1 秘诀12:启动一个辅助线程 40 3.1.2 秘诀13:创建实现runnable接口的activity 44 3.1.3 秘诀14:设置线程优先级 45 3.1.4 秘诀15:取消线程...

    疯狂Android讲义源码

     13.1.4 加入多线程 483  13.2 使用URL访问网络资源 488  13.2.1 使用URL读取网络资源 489  13.2.2 使用URLConnection  提交请求 490  13.3 使用HTTP访问网络 496  13.3.1 使用HttpURLConnection 496  ...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

    精通Android 3 (中文版)2/2

    11.1.5 解决多线程问题  11.1.6 有趣的超时  11.1.7 使用HttpURLConnection  11.1.8 使用AndroidHttpClient  11.1.9 使用后台线程(AsyncTask)  11.1.10 使用AsyncTask处理配置更改  11.1.11 使用...

Global site tag (gtag.js) - Google Analytics