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. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.2

    The elementary tensors $x\otimes \cdots \otimes x$, with all factors equal, are all in the subspace ...

  2. (转载)linux环境变量

    转自:http://www.cnblogs.com/growup/archive/2011/07/02/2096142.html Linux 的变量可分为两类:环境变量和本地变量 环境变量,或者称为全 ...

  3. Flume 与Kafka区别

    今天开会讨论日志处理为什么要同时使用Flume和Kafka,是否可以只用Kafka 不使用Flume?当时想到的就只用Flume的接口多,不管是输入接口(socket 和 文件)以及输出接口(Kafk ...

  4. codeforce 609A - USB Flash Drives

    排序水题 #include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm&g ...

  5. Eclipse关联JavaDoc和源代码

    1.Eclipse 关联 JavaDoc 1.在 Eclipse 菜单中点击 Window -> Preferences,在弹出框中左侧选择展开 Java 节点,点击 Installed JRE ...

  6. HDU-2262 Where is the canteen 概率DP,高斯消元

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2262 题意:LL在一个迷宫里面转,每次走向周围能走的点的概率都是一样的,现在LL要随机的走到cante ...

  7. jqgrid 中设置列不排序

    背景 今天在做系统的功能时,当时有这么个需求:在添加了一行数据时,原本的排序的自动就不能再排序,也就是排序失效. 1. 使用onSortCol事件禁止排序列 当时使用了初始化时,使用onSortCol ...

  8. connect to a specific wifi network in Android programmatically

    http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-prog ...

  9. 转载 ASP.NET中如何取得Request URL的各个部分

    转载原地址 http://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequ ...

  10. 利用.htacess 实现重定向

    步骤: 在网站目录下加入 .htaccess 文件中写 RewriteEngine On RewriteRule ^.*$ index.php 表示开启重写机制 重写到 index.php 的文件