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. Web API-如何将Controller的返回值转换成HTTP response消息

    https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization https://co ...

  2. Android_1_渐变背景色

    首先创建一个渐变背景色文件drawable-mdpi/bg_color.xml <?xml version="1.0" encoding="utf-8"? ...

  3. centos ssh 免密码登录

    最近在学习的过程中遇到这个问题: A主机和B主机: A 免密码登录B: 首先在A的 ~/.ssh 目录中 执行 ssh-keygen -t rsa 一路回车 最后生成连个文件: 将id_rsa.pub ...

  4. Excel2010: Excel使用小技巧(不断更新)

    目录 1. 如何设置定时保存2. 调出“开发工具”功能区3. 在Excel单元格中输入对号和错号4. 设置单元格自动换行5. 向Excel中导入.txt文件6. 如何批量删除超链接7. 如何冻结窗格8 ...

  5. Samung Galaxy III I535 黑砖救活经过

    昨天不小心把手机给搞成黑砖了,如下是昨天发帖求助,结果还没审核过,晕. http://bbs.zhiyoo.com/forum.php?mod=viewthread&tid=9673138&a ...

  6. [九度OJ]1113.二叉树(求完全二叉树任意结点所在子树的结点数)

    原题链接:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...

  7. AVD Snapshot功能

    写程序的时候,经常会碰到:The application has stopped unexpectly… 有时候,会想对原来软件增加新功能或者修改bug.在eclipse修改后保存代码(注意,要保存所 ...

  8. 应用web框架模块设计三国演义篇--转至微博

    从事web开发已经10年时间,近几年也一直从事微博应用产品的研发.从原生php写网站到使用cms bbs整合的大型站点,从使用各种流行的开源开发框架到成熟稳定的平台级框架下做研发.这期间对应用型web ...

  9. altium designer 里如何设置PCB默认字符默认大小(PCB丝印)

    注意:此操作只对新导入的元件生效.      

  10. 大数记录之,大数乘整型数nyoj832

    想到了一个题目:对决二http://acm.nyist.net/JudgeOnline/problem.php?pid=832 但是发现有一道题目是相似的:http://acm.nyist.net/J ...