- 浏览: 157223 次
- 性别:
- 来自: 深圳
最新评论
-
yong7356:
学习了。。。。。。。。。。。。
Android 如何建立AIDL -
910310lenfeng:
小有收获
Paint方法setXfermode -
ihopethatwell:
修改/data/data 权限,修改那几处?
Android——init.rc脚本 -
lyltiger:
http://www.qianxk.com wifi分析
Android Wifi模块分析(二) -
liuborama:
...
Android2.2下实现Ethernet上网
http://www.cnblogs.com/salam/archive/2010/11/10/1873654.html
Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。
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>
发表评论
-
Android获取其他包的Context实例
2011-10-10 07:08 1398Android获取其他包的Context实例然后干坏事 ... -
Intent调用大全
2011-07-25 15:13 819http://www.android123.com.cn/an ... -
Android HAL 开发 (1)
2011-06-27 07:11 1122Android HAL 开发 (1) 2010 ... -
Android获取已安装应用信息(图标,名称,版本号,包)
2011-06-17 10:43 3089http://www.cnblogs.com/mainroad ... -
三种获取系统时间函数的差别
2011-06-16 15:09 2372import android.os.SystemClock; ... -
Android 如何建立AIDL
2011-06-08 15:53 7545这篇文章写的超详细,超好。 在此感谢原作者。 http ... -
How to use Sensor Simulator in Android SDK Emulator
2011-06-07 22:58 2321How to use Sensor Simulator in ... -
解决了google键盘存在触摸识别误差问题
2011-06-02 16:59 975解决了google键盘存在触摸识别误差问题 触摸一个键的顶部 ... -
获取屏幕宽度和高度并判断横竖屏
2011-06-01 10:38 2031void f(){ Display display = (( ... -
最简单的数据库操作
2011-06-01 10:36 884import android.provider.Setting ... -
SystemClock.sleep和Thread.sleep的区别
2011-05-24 11:04 4283在Java中我们处理线程同步问题时,处理延迟可能会使用T ... -
Android 防止手机休眠
2011-05-16 15:53 1508Android 防止手机休眠 在开发Androi ... -
广播发送与接收的简单实现
2011-05-14 15:03 983a.java Intent intent = n ... -
悬浮窗口的实现原理,给出简单示例代码
2011-05-06 13:12 4260本文来自:http://www.eoeandroid.com/ ... -
Android应用程序组件间通信(二)——IntentFilter类简介
2011-04-16 11:17 1748当Intent在组件间 ... -
Android 正则表达式学习
2011-04-13 19:13 8598Android 正则表达式学习 Java正则表达式学习: 因 ...
相关推荐
android Toast大全(五种情形)建立属于你自己的Toast
androidToast大全(五种情形)[归类].pdf
Toast各种类型与颜色的详解,可以适合大多数UI爱好者的使用
Android Toast 大全 (五种形式) 建立属于自己的 Toast
Android Toast 自定义背景、图片 随心使用
Android Toast 工具类
主要为大家详细介绍了android自定义Toast设定显示时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
全局Toast工具类,作用:连续显示toast提示时取消上一个toast 通过ToastUtil.toast(Context(), "文本")调用
一个简单的自定义Toast资源,您可以根据自己的需要更改我的代码即可实现您想要的效果(主要是更改xml文件的布局),这是androidstudio的项目,但是不妨碍在eclipse中使用,可以直接在eclipse中新建android项目,然后将...
安卓Android源码——五种不同的Toast效果.zip
最近在开发中我们经常会在适配5.0以后的机型遇到各种各样的问题,其中有一个不大不小的问题就是:Toast不显示问题,这篇文章就给大家总结了Android 5.0以上Toast不显示的原因与解决方法,有需要的朋友们可以参考借鉴...
Android应用源码之五种效果的Toast.zip
React Native Toast 一个用于android和ios类似于android toast提示控件,android使用原生toast, ios使用UIView Toast
Android Toast各种使用方法及DEMO
小米mimu系统,会对toast进行拦截,在用户提交的toast 消息体拼接一个前缀,由于该种会导致插件工程出现资源错乱,获取appLabel异常,现我们通过hook 动态代理,对消息发送做一个劫持,修改消息信息,还原原来的消息
android Toast对象的使用 自定义Toast
c# winform 类似android toast消息功能 超级强大,多个项目在用,可插拔,封装好了,拿去就可以用了
Demo android service toast 第一个版本