xml文件:

main1:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.toastdemo.MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带有图片的toast"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义的Toast"
android:textSize="20sp"/> </LinearLayout> </RelativeLayout>

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义内容"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/> </LinearLayout>

源代码:

package com.example.toastdemo;

import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
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.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast; public class MainActivity extends Activity { private Button btn_1;
private Button btn_2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2); /**
* 显示带有图片的Toast
*/
btn_1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(MainActivity.this, "show Toast with the picture", Toast.LENGTH_SHORT);
LinearLayout toastLayout = (LinearLayout) toast.getView();
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
toastLayout.addView(image,0);//设置图片于文字上
//设置toast显示的位置,其中X方向偏移量左-右+,y方向的偏移量上-下+
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
/**
* 显示自定义的Toast
*/
btn_2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = new Toast(MainActivity.this);
LayoutInflater layout_inflate = LayoutInflater.from(MainActivity.this);
View inflater = layout_inflate.inflate(R.layout.main2, null);
        
//设置自定义的视图,并展示
              toast.setView(inflater);
                toast.show();
}
});
}
}

Android_Toast的更多相关文章

  1. android 开发-Toast控件的实现

    Toast吐司: Toast内容简单,不做过多介绍,Toast支持自带简单吐司,自定义吐司.内容简单可见代码,详见API.A toast provides simple feedback about ...

随机推荐

  1. e2e 自动化集成测试 环境搭建 Node.js Selenium WebDriverIO Mocha Node-Inspector

    Node.js已经出来了许多年载,至今才开始接触.周未在家闲来无事,一时心血来潮,Google了大量的文章,经过实验,终于可以把整个环境给搭起来, 废话不多话,请看步骤. 特别注意, 本文章是针对Wi ...

  2. 【转】修改xampp的mysql默认密码

    http://www.cnblogs.com/hongchenok/archive/2012/08/21/2648549.html MySQL 的“root”用户默认状态是没有密码的,所以在 PHP ...

  3. 线性存储结构-LinkedList

    LinkedList内部采用链表的形式构建,是一个双向链表.除了继承List外,还继承了Deque接口,可以当做堆栈结构使用. private static final class Link<E ...

  4. IE兼容性问题解决方案2--css样式兼容标签

    在页面中加入下面的标签: <meta http-equiv = "x-UA-compatible" content="IE=edge,chrome=1"& ...

  5. HLA中常用的基本术语

    (1)联邦(Federation):用于实现某一特定仿真目的的分布仿真系统. (2)联邦成员(Federate):参与联邦的所有应用都称为联邦成员,简称成员. (3)对象(Object):构成成员的基 ...

  6. 单点登录sso规范

    http://jasig.github.io/cas/development/protocol/CAS-Protocol-Specification.html

  7. HW7.10

    public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...

  8. HDU-4662 MU Puzzle 水题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 倒推考虑长度就可以了. //STATUS:C++_AC_31MS_240KB #include ...

  9. Win7旗舰版的nfs服务器如何架设? - Microsoft Community

    Win7旗舰版的nfs服务器如何架设? - Microsoft Community Win7旗舰版的nfs服务器如何架设?

  10. 射频识别技术漫谈(3)——能量、调制【worldsing 笔记】

    无源和免接触是非接触式IC卡相对于接触式IC卡的两大特点.无源是指卡片上没有电源,免接触是指对卡片的读写操作不必和读写器接触.非接触式智能卡也是IC卡,而卡上的IC即集成电路工作时肯定是需要电源的,卡 ...