• Toast吐司:

  Toast内容简单,不做过多介绍,Toast支持自带简单吐司,自定义吐司。内容简单可见代码,详见API。A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

  •   代码示例

  activity_main.xml

    

 <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=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="190dp"
android:text="吐司按钮" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="20dp"
android:text="自定义吐司" /> </RelativeLayout>

  toast_layout.xml

    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"> <ImageView android:src="@drawable/droid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>

  Activity

 package com.example.android_toast;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author xiaowu
* NOTE:Toast吐司,支持自带简单吐司,自定义吐司。内容简单可见代码,详见API
*/
public class MainActivity extends Activity {
private Button button ;
private Button button2 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button2 = (Button)this.findViewById(R.id.button2); //为按钮添加点击监听事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this, "吐司内容", 1).show();
//创建吐司对象
Toast toast =Toast.makeText(MainActivity.this, "吐司内容", 0);
//设置吐司在视图中显示的位置
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
toast.show();
}
}); //为按钮添加点击监听事件
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//通过xml配置文件,加载自定义吐司视图
View view = getLayoutInflater().inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
//通过该视图查找视图中的对象textView
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText("吐司内容");
// Toast toast = new Toast(MainActivity.this);
Toast toast = new Toast(getApplicationContext());
//设置吐司在视图中显示的位置
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
//设置吐司限时时长
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

android 开发-Toast控件的实现的更多相关文章

  1. CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建

    Android开发使用控件入门--环境搭建 2014-12-24 09:57     14人阅读     评论(0)     收藏         编辑     删除 CAD控件.CAD三维控件,手机 ...

  2. Android开发使用控件入门--环境搭建

    Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD  ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...

  3. 023 Android 自定义Toast控件

    1.Toast自定义控件工具类 package com.example.administrator.test62360safeguard.Utils; import android.content.C ...

  4. Android开发CheckBox控件,全选,反选,取消全选

    在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...

  5. Android开发ImageView控件缩放图片

    首先还是最基础的ImageView控件如何显示图片: <ImageView                Android:id="@+id/imgView"          ...

  6. android 开发-设置控件/view的水平方向翻转

    设置控件沿着水平方向翻转(即Y轴180°) 看效果: 代码: <pl.droidsonroids.gif.GifImageView android:id="@+id/gv_image1 ...

  7. android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法

    1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...

  8. 【Android开发】控件外边框自定义

    1.在drawable里面新建自定义的资源文件shape <?xml version="1.0" encoding="utf-8"?> <sh ...

  9. 从零开始学android开发-获取控件

    mBtnNews = (Button)findViewById(R.id.btn_news);//获取控件

随机推荐

  1. Python-socket发送文件并解决粘包问题

    服务器端要先根据客户端要下载的文件进行判断是否存在,还要根据文件大小来进行传送,最后还要比对文件的md5值来判断传送的文件是否正确,通过判断剩余字节来解决粘包问题 服务器端 # -*- coding: ...

  2. zabbix 系列 (1)安装

    安装server http://blog.csdn.net/xiegh2014/article/details/54988548 安装 agent http://m.blog.csdn.net/wu2 ...

  3. Linux下压缩/解压

    Linux下各种压缩包的解压方法 作者:intq 时间:2009-9-25 文章来源:来自网络 ---------------------------------------------------- ...

  4. 并发设计模式和锁优化以及jdk8并发新特性

    1 设计模式 (1) 单例模式 保证一个类只能一个对象实现.正常的单例模式分为懒汉式和饿汉式,饿汉式就是把单例声明称static a=new A(),系统第一次调用的时候生成(包括调用该类的其他静态资 ...

  5. 嵌入式linux环境搭建

    花了两天时间,终于搭建好了板子上的linux驱动开发环境,不容易呀,做个笔记. 首先搭建PC上的编译环境,因为编译的驱动是在板子上运行的,第一步当然需要安装交叉编译器,即arm-none-linux- ...

  6. 【机器学习】推荐系统、SVD分解降维

    推荐系统: 1.基于内容的实现:KNN等 2.基于协同滤波(CF)实现:SVD → pLSA(从LSA发展而来,由SVD实现).LDA.GDBT SVD算是比较老的方法,后期演进的主题模型主要是pLS ...

  7. Spring入门第三十课

    基于XML的方式配置事务 直接看代码: package logan.study.spring.tx.xml; public interface BookShopDao { //根据书号获取书的单价 p ...

  8. Free Style Structure text

    最近用了很多文本配置输入,考虑一个最简单的格式,适合C语言scanf读写数据. 基本数据类型直接使用常用形式. double 直接用小数,对应%f读取. int 直接用十进制整数,对应%d读取. ch ...

  9. 【C#】截取字符串

    几个经常用到的字符串的截取 string str="123abc456"; int i=3; 1 取字符串的前i个字符 str=str.Substring(0,i); // or ...

  10. python drift

    install dependency(optional):(本文来自 不才b_d 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/sinat_36184075/arti ...