- 浏览: 109080 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
zhangchixtacbn:
能否发一份代码参考一下,谢谢。554242516@qq.com ...
DWZ整合struts2笔记 -
tuimaochang:
你好,这个有完整的源码吗?里面的对话框/图片资源用我自己的看不 ...
ActivityGroup + ViewPager 实现可滑动的底部Tab -
lintghi:
...
教你如何提高eclipse速度 -
119568242:
...
解决Bitmap读取频发OOM -
thunder_yan:
beat_it_ 写道能不能给份代码啊?上面不就是一个例子吗? ...
DWZ整合struts2笔记
Toast用于向用户显示一些帮助/提示。下面展示了5种效果,来说明Toast的强大。
注意:
LENGTH_LONG---长时间显示视图或文本提示
LENGTH_SHORT---短时间显示视图或文本提示
setGravity(int gravity,int xOffset,int yOffset)---设置提示应该在屏幕上的显示的位置
setDuration(int duartion)---设置提示显示的持续时间
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.其他线程
完整代码
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>
评论
2 楼
119568242
2012-07-31
=-=没看到handler
1 楼
119568242
2012-07-31
=-=我怎么记得好像Toast只能主线程调用?= =?其他线程会报错- -
发表评论
-
Android 实现圆形Panel ListView
2012-12-04 18:32 1239(效果如上图所示) 其实很简单: 比方说上面的容 ... -
Android WIFI热点工具
2012-10-28 02:09 3370Hello Thunder夜未眠, 午夜时分, 发布一个小工具 ... -
Android 2.3+ 使用StrictMode
2012-09-21 14:47 0ANR窗口产生的原因是多种多样的。程序的主线程因为IO读 ... -
内存泄露 on Android
2012-09-21 14:41 10471.资源对象没关闭造成的内存泄漏 描述: 资源性对象 ... -
Android 高效编程
2012-09-21 14:36 13071.使用本地方法 当 ... -
Android, BaseAdapter 处理大数据量时的优化
2012-09-21 14:23 7317Android优化 最常见的就是ListView, Galle ... -
SQLite优化方法
2012-09-21 13:50 34141.建表优化 SQLite的数据库本质文件读写操作,频 ... -
Android SQLite插入优化
2012-09-21 13:48 1265最初代码如下,直接执行sql语句,外加事务提升性能: ... -
解决Bitmap读取频发OOM
2012-08-25 09:48 1218/** * Bitmap 工具包 * @author ... -
ActivityGroup + ViewPager 实现可滑动的底部Tab
2012-07-01 17:28 6822首先看看布局文件 main.xml <?xml ... -
Android 使用des算法
2012-06-11 14:09 1852import java.security.Key; i ... -
Android TabActivity中onKeyDown无效问题
2012-06-11 14:04 34871.当继承TabActivity时,同学们是不是onKeyDo ... -
Android程序安装和卸载
2012-06-11 14:01 998安装: String str = "/Can ... -
Android 完全退出
2012-06-11 14:00 2496//通过context获取系统服务,得到Activity ... -
Android:创建网格状的RadioGroup
2012-06-05 10:37 3386Android系统自带的Radio ... -
Android: NDK编程
2012-06-05 10:32 5692为何要用到NDK? 概括来说主要分为以下几种情况: ... -
Android 3D图片切换
2012-06-05 10:25 2431MainActivity package org.wp ... -
android反编译和防止反编译的方法
2012-06-05 10:23 1485android基于java的,而ja ... -
android开发之MediaPlayer+Service MP3播放器
2012-06-02 14:57 3909import java.io.File; impo ... -
Android怎么让一个service开机自动启动
2012-06-02 14:58 1839今天我们主要来探讨android怎么让一个service开机自 ...
相关推荐
主要为大家详细介绍了android自定义Toast设定显示时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
一个简单的自定义Toast资源,您可以根据自己的需要更改我的代码即可实现您想要的效果(主要是更改xml文件的布局),这是androidstudio的项目,但是不妨碍在eclipse中使用,可以直接在eclipse中新建android项目,然后将...
Android 源码演示5种toast显示效果,一共是以下几种演示:默认的Toast显示、自定义位置的Toast显示、带图片的Toast显示、完全自定义的Toast显示、长时间的Toast显示,Android 自定义5种toast显示效果。有的Toast效果...
Android Toast 自定义背景、图片 随心使用
android 自定义toast,toast自定义显示时间,toast自定义显示样式
/* 显示toast,自己定义显示长短。 * param1:activity 传入context * param2:word 我们需要显示的toast的内容 * param3:time length long类型,我们传入的时间长度(如500)*/
Android中定义了一个Toast对象,用以弹出一个窗口来给予用户帮助和提示,和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用户的焦点,在弹出Toast的时候,依然可以对之前的界面进行操作,我们在“”...
主要介绍了Android 自定义 Toast 显示时间的相关资料,需要的朋友可以参考下
Android中自定义Toast.pdf 学习资料 复习资料 教学资源
Android自定义Toast提示信息工具类,可以实现如下功能: 1、最简单Toast显示 2、自定义图标、图标颜色 3、自定义文本颜色 4、自定义背景颜色 5、设置组件水平、垂直显示位置 6、设置边框宽度、边框颜色
Android:实现Toast自定义样式(包括:自定义位置、带图片等)、自定义显示时长(包括可用系统时长、可自定义)
默认Toast(大家都会),自定义Toast位置,自定义Toast样式
android Toast对象的使用 自定义Toast
自己整理的自定义控件方法,看了你就会做自定义Dialog和Toast了,没有多余代码,很简洁。背景、按钮都能自定义
自定义Toast,类似Android的Toast
android利用布局文件自定义Dialog、Toast
android自定义时长Toast,Toast 显示时间可以自己设定