android 开发-Toast控件的实现
- 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控件的实现的更多相关文章
- CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建
Android开发使用控件入门--环境搭建 2014-12-24 09:57 14人阅读 评论(0) 收藏 编辑 删除 CAD控件.CAD三维控件,手机 ...
- Android开发使用控件入门--环境搭建
Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...
- 023 Android 自定义Toast控件
1.Toast自定义控件工具类 package com.example.administrator.test62360safeguard.Utils; import android.content.C ...
- Android开发CheckBox控件,全选,反选,取消全选
在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...
- Android开发ImageView控件缩放图片
首先还是最基础的ImageView控件如何显示图片: <ImageView Android:id="@+id/imgView" ...
- android 开发-设置控件/view的水平方向翻转
设置控件沿着水平方向翻转(即Y轴180°) 看效果: 代码: <pl.droidsonroids.gif.GifImageView android:id="@+id/gv_image1 ...
- android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法
1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...
- 【Android开发】控件外边框自定义
1.在drawable里面新建自定义的资源文件shape <?xml version="1.0" encoding="utf-8"?> <sh ...
- 从零开始学android开发-获取控件
mBtnNews = (Button)findViewById(R.id.btn_news);//获取控件
随机推荐
- RS485总线防雷保护方案
RS485作为最为最常用的电表通讯方式之一.日常生活中雷电和静电干扰已经成为485通信总线在实际工程经常遇到的问题.故如何对芯片以及总线进行有效的保护,是摆在每一个使用者面前的一个问题.在这里,我们主 ...
- 在vue项目中使用md5加密
MD5:信息-摘要算法,是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式 一般我们把登录和注册信息的密码进行加密 1.安装模块 cnpm install js ...
- List for game to play latter
1.The Elder Scrolls V 2.Border Lands 1,2 3.Mind Killer 4.Dark Soul 2 5.Watch Dog 6.Valkyria Chronicl ...
- [51nod1138]正整数分解为几个连续自然数之和
解题关键:注意为什么上界是$\sqrt {2n} $ 因为函数是关于m的递减函数,而结果必须为正整数 $a = \frac{{2n + m - {m^2}}}{{2m}} = \frac{n}{m} ...
- 树莓派 Learning 002 装机后的必要操作 --- 04 添加软件源 之 添加公钥 --- 解决“由于没有公钥,无法验证下列签名”问题
树莓派 装机后的必要操作 - 添加软件源 解决 添加公钥 时会遇到的问题 当你添加完Debian的软件源后,在终端中执行sudo apt-get update时,会出现下面的错误:(这里我添加了3个软 ...
- Python脚本打包为exe文件
本文转载自http://www.open-open.com/lib/view/open1342675735745.html 把用Python写好的脚本,可以用pyinstaller打包成.exe文 ...
- Torando 入门
1. 前言 Tornado 是使用 Python 编写的一个强大的.可拓展性的 Web 服务器/框架.与其他主流 Web 服务器框架有着明显区别:Tornado 支持异步非阻塞框架.同时它处理速度非常 ...
- ubuntu下php7安装及配置
直接用apt-get 失败 在官网下安装包http://tw2.php.net/get/php-7.0.18.tar.bz2/from/a/mirror 进行make时 出现错误: libtool: ...
- C# EventHandler委托事件小结--百度
最近遇到一个委托的问题,+=这个符号 this.Activated += new EventHandler(Form1_Activated);//Form1_Activated为方法名12 这个语句拆 ...
- cinder介绍及使用lvm本地存储
1.cinder简介 Cinder提供持久的块存储,目前仅供给虚拟机挂载使用.它并没有实现对块设备的管理和实际服务,而是为后端不同的存储结构提供了统一的接口,不同的块设备服务厂商在 Cinder 中实 ...