一般情况下,我们使用Toast默认的风格就行了,只是有些时候为了达到我们自己想要的效果需要自定义一下,包括自定义显示的位置,显示的内容以及完全自定义里面的布局,代码如下:

activity:

package com.home.testtoast;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener {
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private static final String TOASTBTN_1 = "这是默认的Toast显示";
private static final String TOASTBTN_2 = "这是自定义位置的Toast显示";
private static final String TOASTBTN_3 = "这是带图片的Toast显示";
private static final String TOASTBTN_4 = "这是完全自定义的Toast显示";
private Toast toast = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.main_btn1);
btn2 = (Button) findViewById(R.id.main_btn2);
btn3 = (Button) findViewById(R.id.main_btn3);
btn4 = (Button) findViewById(R.id.main_btn4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v == btn1) {
toast.makeText(this, TOASTBTN_1, Toast.LENGTH_LONG).show();
}
if (v == btn2) {
toast = Toast.makeText(getApplicationContext(), TOASTBTN_2,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
if (v == btn3) {
toast = Toast.makeText(getApplicationContext(), TOASTBTN_3,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 50, -100);
LinearLayout layout = (LinearLayout) toast.getView();
ImageView image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.image1);
layout.addView(image, 0);
toast.show();
}
if (v == btn4) {
View view = getLayoutInflater().inflate(R.layout.userdefinedtoast,
(ViewGroup) findViewById(R.id.toast_layout));
TextView txtView_Title = (TextView) view
.findViewById(R.id.txt_Title);
TextView txtView_Context = (TextView) view
.findViewById(R.id.txt_context);
ImageView imageView = (ImageView) view
.findViewById(R.id.image_toast);
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
}
} }

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/main_btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认的Toast" /> <Button
android:id="@+id/main_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="自定义位置的Toast" /> <Button
android:id="@+id/main_btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="带图片的Toast" /> <Button
android:id="@+id/main_btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="完全自定义的Toast" /> </LinearLayout>

自定义的Toast布局:userdefinedtoast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout"
android:layout_width="200dip"
android:layout_height="fill_parent"
android:background="#111111"
android:orientation="vertical" > <TextView
android:id="@+id/txt_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:text="自定义Toast"
android:textColor="#ffffff"
android:textSize="20dip" >
</TextView> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#999999"
android:orientation="horizontal" > <ImageView
android:id="@+id/image_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:src="@drawable/image2" >
</ImageView> <TextView
android:id="@+id/txt_context"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="这是自定义Toast,左边的是一幅很美的图片"
android:textColor="#ffffff"
android:textSize="15sp" >
</TextView>
</LinearLayout> </LinearLayout>

附上图片效果:

几种不同风格的Toast的更多相关文章

  1. jmeter随笔(9)--有两种编码风格,导致数据乱码

    问题:在一个网站,有两种编码风格,导致数据乱码 解决办法: 1.首先设置jmeter的配置文件 2.针对要求是utf-8格式的这样的请求,做单独的编码处理(beanshell处理) 3.运行,在htm ...

  2. 何谓Dandy?它是一种着装风格

    何谓Dandy?它是一种着装风格_女性_腾讯网 何谓Dandy?它是一种着装风格 2012年02月17日09:47腾讯专稿我要评论(0) 字号:T|T   何谓Dandyism?它是一种风格,词根Da ...

  3. ToastCustom【自定义显示风格的Toast】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 基于系统Toast的自定义显示风格的Toast. 效果图 代码分析 ToastCustom类基于系统Toast,不是继承Toast, ...

  4. ExtJS中layout的12种布局风格

    总览 extjs的容器组件都可以设置它的显示风格,它的有效值有 1. absolute,2. accordion, 3. anchor, 4. border, 5. card, 6. column, ...

  5. 一个Java应用,三种字体风格(Java, Windows, Mac),真是蛋疼

    大家看看下面这张图,红色圈起来的是Java难看至极的字体渲染,黄色圈起来的是正常的Windows渲染,绿色是Mac风格的渲染. 其实我感觉正常风格就挺好的,就是看那个Java的Swing菜单,非常不顺 ...

  6. jeecg 3.5.2 新版本号4种首页风格 【经典风格,shortcut风格,ACE bootstrap风格,云桌面风格】

    [1]经典风格: [2]Shortcut风格: [3]ACE bootsrap风格: [4]云桌面风格: [5]自己定义图表 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  7. android自定义风格的toast

    先上图看一下我的自定义toast的样式 源码下载地址: CustomToastActivity.java源码 package com.jinhoward.ui_customtoast; /** * A ...

  8. 使用vim正则表达式删除C/C++注释 及 两种注释风格替换

    /*对于C风格的注释可以使用如下命令*/ :%s/\_s*\/\*\(\S\|\_s\)\{-}\*\///g //对于C++风格注释 :%s/\/\/.*//g /*...*/ -> //.. ...

  9. 有空研究一下OwnerDraw和三种Windows风格CS_OWNDC和CS_PARENTDC和CS_CLASSDC

    就在StdCtrls.pas, ExtCtrls.pas和Buttons.pas三个类里研究就够了,这是初步的搜索结果: ---------- 在打开的文档中查找 ---------- "G ...

随机推荐

  1. 【javascript 动态添加数据到 HTML 页面】

    今天简单的学习了一下有关对象字面量的定义和 javascript 如何取出对象字面量的值的知识,javascript 动态添加数据到 HTML 页面的问题. [学习目标]有如下的一组数据通过 Ajax ...

  2. nginx 反向代理 odoo, 速度提升

    1: sudo apt-get  install nginx 2:  add server: 在/etc/nginx/sites-enabled 中增加一个可用的server. 文件如下图所示 3:重 ...

  3. MOS管体二极管的作用

    这里有两种解释: 1.mos管本身自带有寄生二极管,作用是防止VDD过压的情况下,烧坏mos管,因为在过压对MOS管造成破坏之前,二极管先反向击穿,将大电流直接到地,从而避免MOS管被烧坏. 2.防止 ...

  4. App在后台运行

    App有三种状态: 1. 死亡状态(未打开App); 2. 前台运行状态(打开状态); 3. 后台暂停状态(停止所有动画, 定时器, 多媒体联网等操作) 4. 后台运行状态(后台运行); ------ ...

  5. JavaScript高级之闭包的概念及其应用

    主要内容: 什么是闭包 闭包使用的一般模式 闭包都能做些什么 本文是我的JavaScript高级这个系列中的第二篇文章. 在这个系列中,我计划分析说明 一下JavaScript中的一些常用的而又神秘的 ...

  6. JavaScript学习代码整理(二)--函数

    //JavaScript函数 //简单的求和函数 function sum(a,b) { return a + b; } //函数可以存储在变量中,也可以通过变量调用函数 x = sum(a,b); ...

  7. javascript 字符串转数字

    //把str转换为数字的方式,想起很久以前的一个面试题,说字符转数字的方式有哪些,现在想了想 var str1='4.88',str2='4.8xx'; console.log(parseInt(st ...

  8. 【HDOJ】1963 Investment

    完全背包. #include <stdio.h> #include <string.h> #define max(a, b) (a>b) ? a:b ], an[]; ] ...

  9. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  10. opengl学习

    #include"stdafx.h" #define GLUT_DISABLE_ATEXIT_HACK #include <GL/glut.h> //glut自动将gl ...