- 浏览: 478504 次
- 性别:
- 来自: 北京
最新评论
-
fatalfeel:
Irrlicht 3d Engine is full open ...
转:Android世界的15款开源的游戏开发引擎 -
diyangxia:
BigDecimal怎么初始化全局变量呢
android如何保留小数点后x位数字 -
c1230v:
这是一个程序员应该用的方法吗?求靠谱一点
转:android WebView 控件加载本地sdcard中html文件图片的问题 -
dingbuoyi:
楼主是天才 真牛逼
转:Progressbar-设置自己的样式 -
panxiaoming2008:
记住:显示本地文件时 必须使用APK'>file:/// ...
Android使用WebView加载本地资源
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();
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
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;
}
}
}
<?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>
<?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>
源码见附件
- CustomToast.rar (11.4 KB)
- 下载次数: 5
发表评论
-
service学习
2012-05-16 00:41 848http://www.cnblogs. ... -
InputStream,String,File相互转化
2012-05-14 23:32 12471. String --> InputStream ... -
转:实现类似手机QQ的可折叠固定标题列表
2012-04-12 19:05 1360http://douzifly.cnblog ... -
转:[Android]倒影效果的ImagView
2012-04-06 23:29 1349本文源自:http://blog.csdn.net/ke ... -
转:JDK中的URLConnection参数详解
2012-04-06 23:01 1150针对JDK中的URLConnection连 ... -
转:Game Engines for Android (Android游戏引擎)
2012-03-24 13:12 2691With Google IO fast approach ... -
做Android 系统/应用开发怎么可以不懂自动化测试
2012-03-22 15:02 1118http://blog.csdn.net/yiyaaixuex ... -
让Qt应用程序跑在Android上
2012-03-22 14:55 1323http://blog.c ... -
转:在android 2.3 AVD 模拟器上安装 google market 安卓市场
2012-03-22 14:29 1052http:// ... -
增加 addDataScheme("file") 才能收到SD卡插拔事件的原因分析 -- 浅析android事件过滤策略
2012-03-22 14:26 1923... -
转:andengine中的ParticleSystem
2012-01-18 11:08 1500看ParticleSystemSimpleExample,看得 ... -
test4:腾讯
2011-12-26 20:27 2www.qq.com -
test3:Yahoo
2011-12-26 20:25 2www.yahoo.com -
test2:google
2011-12-26 10:53 5www.google.com.hk -
test1:百度
2011-12-26 10:52 6www.baidu.com -
转:android 打包自己的自定义组件成JAR包
2011-12-24 11:54 1249http://www.cnblogs.c ... -
转:AndEngine中的字体加载及应用
2011-12-15 15:39 1520AndEngine中的字体加载及应用 An ... -
转:Android游戏框架AndEngine使用入门
2011-12-13 14:24 1559项目站点:http://www.andengine.org 项 ... -
转:notification更新数字的显示及AnimationDrawable的结合使用
2011-12-07 10:25 2578这是本人加入博客园的首篇博文,自己平常在工作学习过程遇 ... -
转:Android系统上(mv不可用)cp命令的替代方法
2011-11-30 18:32 9633Android系统上cp命令 ...
相关推荐
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 第一个版本