- 浏览: 485842 次
- 性别:
- 来自: 北京
-
最新评论
-
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 895http://www.cnblogs. ... -
InputStream,String,File相互转化
2012-05-14 23:32 12781. String --> InputStream ... -
转:实现类似手机QQ的可折叠固定标题列表
2012-04-12 19:05 1396http://douzifly.cnblog ... -
转:[Android]倒影效果的ImagView
2012-04-06 23:29 1395本文源自:http://blog.csdn.net/ke ... -
转:JDK中的URLConnection参数详解
2012-04-06 23:01 1187针对JDK中的URLConnection连 ... -
转:Game Engines for Android (Android游戏引擎)
2012-03-24 13:12 2729With Google IO fast approach ... -
做Android 系统/应用开发怎么可以不懂自动化测试
2012-03-22 15:02 1162http://blog.csdn.net/yiyaaixuex ... -
让Qt应用程序跑在Android上
2012-03-22 14:55 1372http://blog.c ... -
转:在android 2.3 AVD 模拟器上安装 google market 安卓市场
2012-03-22 14:29 1090http:// ... -
增加 addDataScheme("file") 才能收到SD卡插拔事件的原因分析 -- 浅析android事件过滤策略
2012-03-22 14:26 1976... -
转:andengine中的ParticleSystem
2012-01-18 11:08 1539看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 1283http://www.cnblogs.c ... -
转:AndEngine中的字体加载及应用
2011-12-15 15:39 1560AndEngine中的字体加载及应用 An ... -
转:Android游戏框架AndEngine使用入门
2011-12-13 14:24 1589项目站点:http://www.andengine.org 项 ... -
转:notification更新数字的显示及AnimationDrawable的结合使用
2011-12-07 10:25 2629这是本人加入博客园的首篇博文,自己平常在工作学习过程遇 ... -
转:Android系统上(mv不可用)cp命令的替代方法
2011-11-30 18:32 9676Android系统上cp命令 ...
相关推荐
### Android Toast 大全(五种情形) #### 一、概览 在Android开发中,`Toast`是一种轻量级的提示方式,主要用于快速显示简短的信息,如操作结果、临时提示等。它不会阻塞UI线程,也不需要用户进行任何交互即可...
通过上述五种不同情形的介绍,可以看出Android Toast的使用非常灵活,可以根据不同的需求场景选择合适的Toast使用方式,从而提升应用的用户体验。在实际开发中,开发人员可以根据具体的应用需求,选择合适的Toast...
下面我们将详细讲解如何实现标题中提到的五种`Toast`情形。 1. **默认效果**: 默认的`Toast`样式是系统预设的,只需调用`makeText()`方法并传入上下文、显示文本和持续时间即可。如: ```java Toast.makeText...
Android Toast 大全 (五种形式) 建立属于自己的 Toast
在Android开发中,`Toast`是一种轻量级的提示方式,用于向用户显示短暂的信息,如操作结果或者简单的提示。通常,`Toast`会显示一个简单的文本消息,但默认样式可能无法满足所有设计需求。本篇文章将深入探讨如何在...
Android 自定义 Toast 设定显示时间 Android 自定义 Toast 设定显示时间是指在 Android 应用程序中,自定义 Toast 的显示时间,而不是使用系统默认的 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG。本文将详细介绍如何...
在Android应用开发中,`Android Toast`是一种轻量级的提示机制,用于向用户显示简短的信息,通常在用户操作后出现并自动消失。标题提到的“Android Toast即便关闭了通知权限也会正常显示”是一个关键点,这涉及到...
在Android应用开发中,`AndroidToast`工具类是一种常见的组件,用于向用户显示短暂的通知信息。这些信息通常出现在屏幕上的某个位置,展示几秒钟后自动消失,不会干扰用户的正常操作。`AndroidToast`的使用非常方便...
在Android应用开发中,Toast是一种常用的轻量级提示方式,用于短暂显示消息,告知用户一些信息或者操作结果。然而,系统默认的Toast虽然方便,但其样式和功能相对固定,不能满足所有开发者的需求。在某些情况下,...
本资源“安卓Android源码——五种不同的Toast效果.zip”包含了一个示例项目,展示了如何实现五种不同的Toast效果,这对于开发者来说是很有价值的学习材料。下面我们将深入探讨这些知识点: 1. **基础的Toast使用** ...
这个“Android应用源码之五种效果的Toast.zip”文件包含了一个示例项目,展示了如何自定义`Toast`以实现不同的视觉效果。下面我们将详细探讨`Toast`的基本使用和如何进行自定义。 1. **`Toast`的基本用法** - 创建...
在Android开发中,Toast是一种轻量级的通知方式,它用于显示短暂的信息,通常用来提示用户某个操作的结果。系统默认的Toast显示时间有限,一般分为短时间和长时间两种模式,但有时开发者可能需要更灵活的控制Toast的...
全局Toast工具类,作用:连续显示toast提示时取消上一个toast 通过ToastUtil.toast(Context(), "文本")调用
【C# Winform 类似Android Toast消息功能】是一种在Windows桌面应用中实现类似Android系统Toast功能的技术。在Android中,Toast是一种短暂显示信息的方式,它不会阻断用户与界面的交互,而是在屏幕某一位置短暂展示...
React Native Toast是一个跨平台的组件,它为Android和iOS提供了类似Android原生Toast的功能。这个组件使得开发者在使用React Native进行混合移动应用开发时,能够方便地实现快速、简洁的提示信息显示,无论是在...
在Android应用开发中,`Toast`是常用的轻量级提示组件,用于向用户展示短暂的信息。默认情况下,`Toast`的样式和位置都是系统固定的,但开发者可以根据需求自定义其样式和显示位置,以增强用户体验。本文将详细介绍...
在Android开发中,`Toast`是一个非常常用的组件,它用于向用户显示短暂的消息提示,而不会中断当前应用程序的运行。`Toast`的消息会浮现在屏幕上方或下方,并在指定时间后自动消失,非常适合用于告知用户一些简短的...
在Android开发中,Toast是一种轻量级的通知方式,它用于显示短暂的信息,不会阻断用户当前的操作。默认的Toast在屏幕中央短暂显示文本信息,但有时开发者可能需要更灵活的展示方式,比如改变位置或样式,这就涉及到...
然而,Service通常运行在自己的工作线程中,这意味着在Service中直接使用Toast可能会导致`android.view.ViewRootImpl$CalledFromWrongThreadException`异常。为了解决这个问题,有以下两种常见方法: 1. 使用`...