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);//获取控件
随机推荐
- influxdb api
https://influxdb-python.readthedocs.io/en/latest/examples.html
- cmake opencv,dlib 编译静态库 1
无论windows,linux 所有的库 ,dlib,opencv 通过cmake-gui 设置好静态库, 动态库,和其他各个选项 Tips: cmake 优先级用cmake-gui,因为命令太多,容 ...
- python操作sql server2008 pyodbc
使用Python通过PyODBC连接数据的注意事项 今天使者用PyODBC连接数据库,试了很久才出来,现把一些心得体会和大家分享! 一.PyODBC的下载地址: http://code.google. ...
- 在VirtualBox中安装CentOS 7【转载】
当初接触Linux的时候,因为条件限制,只能在VirtualBox虚拟机中安装Linux系统使用,由于是小白,爬了好多坑.于是决定写一篇关于在虚拟机中安装linux系统的文章.一是为了巩固自己的知识, ...
- Antiprime数-数论
题目描述 Description 如果一个自然数n满足:所有小于它的自然数的约数个数都小于n的约数个数,则称n是一个Antiprime数.譬如:1.2.4.5.12.24都是Antiprime数. ...
- 选择炸了(JIRA)的88个
作者:Martin Seibert SEIBERT MEDIA 首席执行官. 原文地址:http://seibert.biz/jirareasons 作者Martin Seibert 是德国互联网代理 ...
- Angular06 组件、模块、父子组件之间的数据传递
1 创建组件 进入到angular项目的根目录,执行如下命令 ng g component test-component 注意:执行完上述命令后在angular项目的src/app文件夹下就会多出一个 ...
- 38、EST序列拼接流程
转载:http://fhqdddddd.blog.163.com/blog/static/18699154201241014835362/ http://blog.sina.com.cn/s/blog ...
- 内核启动流程3--Busybox的init进程
Busybox是用来制作文件系统的一个工具集,可以用来替换GNU fileutils shellutils等工具集,它为各种小型的或者嵌入式系统提供了比较完全的工具集. 它提供的核心程序中包括了用户空 ...
- ubuntu-12.04.5安装cacti笔记
坑啊,磨磨蹭蹭按了一个星期.按了3个版本. 第一次:cacti-0.8.7e.tar.gz 安装完之后,Host: Localhost->Memory Usage...四张图始终出不了.点击进去 ...