Android Toast 总结(五种用法)
Toast大家都很熟,不多说。直接上图上代码。
具体代码如下:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="5dip" > <Button
android:id="@+id/btnSimpleToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="默认" >
</Button> <Button
android:id="@+id/btnSimpleToastWithCustomPosition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定义显示位置" >
</Button> <Button
android:id="@+id/btnSimpleToastWithImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="带图片" >
</Button> <Button
android:id="@+id/btnCustomToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="完全自定义" >
</Button> <Button
android:id="@+id/btnRunToastFromOtherThread"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="其他线程" >
</Button> </LinearLayout>
custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:orientation="vertical" > <TextView
android:id="@+id/tvTitleToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:background="#bb000000"
android:gravity="center"
android:textColor="#ffffffff" /> <LinearLayout
android:id="@+id/llToastContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dip"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:background="#44000000"
android:orientation="vertical"
android:padding="15dip" > <ImageView
android:id="@+id/tvImageToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" /> <TextView
android:id="@+id/tvTextToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:textColor="#ff000000" />
</LinearLayout> </LinearLayout>
package com.example.test; import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build; public class MainActivity 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.ic_launcher);
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.ic_launcher);
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; } } }
运行即可。
本文转载自:《自定义Toast》
下面再附上:7种形式的Android Dialog使用举例
Android Toast 总结(五种用法)的更多相关文章
- Android系统的五种数据存储形式(一)
Android系统有五种数据存储形式,分别是文件存储.SP存储.数据库存储.contentprovider 内容提供者.网络存储.其中,前四个是本地存储.存储的类型包括简单文本.窗口状态存储.音频视频 ...
- Android Intent的几种用法全面总结
Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...
- C#this的五种用法
this的五种用法: 1.使用被掩盖的成员变量: class AA { int a; public void set1(int a) { this.a = a;//right } public voi ...
- Android系统的五种数据存储形式(二)
之前介绍了Android系统下三种数据存储形式,今天补充介绍另外两种,分别是内容提供者和网络存储.有些人可能认为内存提供者和网络存储更偏向于对数据的操作而不是数据的存储,但这两种方式确实与数据有关,所 ...
- Android数据存储五种方式总结
本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用Cont ...
- Android 数据存储五种方式
1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...
- Android Intent的几种用法总结【转】
Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料.都指定好后,只要调用startAc ...
- webpack解惑:require的五种用法
我之前在 <前端搭环境之从入门到放弃>这篇文章中吐槽过,webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require ...
- webpack解惑:require的五种用法 (转)
我之前在 <前端搭环境之从入门到放弃>这篇文章中吐槽过,webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require ...
随机推荐
- HDU-1007 Quoit Design 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...
- Gym 100827G Number Game (博弈)
Number Game Alice and Bob are playing a game on a line of N squares. The line is initially populated ...
- Java之字节输入流和输出流
package IODemo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...
- C++为QLabel增加单击事件
原文来源: http://www.cnblogs.com/findumars/p/4058379.html 原理: 其实就是改写了一个函数:mouseReleaseEvent,当在QLabel放开鼠标 ...
- 基于redis的IP地址快速查询
在一些大数据处理中,我们需要用到IP地址查询,一般为了查询一个IP属于哪个地址,我们通常需要根据一个IP数据库来查询,网络上比较常用的IP库是纯真IP数据库.IP数据库里面的记录一般存储方式为IP的开 ...
- Keil: warning: A1581W: Added 2 bytes of padding at address
KEIL MDK编译警告: warning: A1581W: Added 2 bytes of padding at address xxx 原因: 在Keil 里写汇编代码时如果代码尺寸不对齐, ...
- jquery-ui 之droppable详解
<div class="special">drop me</div> <div class="drag-box"> < ...
- 好的 ASP.Net网站、博客
1.优秀的个人技术博客 http://www.tracefact.net/Asp-Net/
- Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...
- int、bigint、smallint 和 tinyint
Transact-SQL 参考 int.bigint.smallint 和 tinyint 使用整数数据的精确数字数据类型. bigint 从 -2^63 (-9223372036854775808) ...