Android开发学习之Toasting应用与实例
2014-04-23 15:16
429 查看
一.Toast的简介
Toast是Android中一种提供给用户简短信息的视图,该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能已不显眼的方式,使用户看到你提供的信息。显示的时间是有限制的,过一段时间后会自动消失,不过Toast本身可以控制显示时间的长短。
二.Toast的常用方法
三.Toast的不同显示样式
效果图(有五种不同的Toast显示样式):
![](http://img.blog.csdn.net/20140424124156250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGU5MDIyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
main.xml
toast.xml
strings.xml
ToastDemoActivity.java
Toast是Android中一种提供给用户简短信息的视图,该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能已不显眼的方式,使用户看到你提供的信息。显示的时间是有限制的,过一段时间后会自动消失,不过Toast本身可以控制显示时间的长短。
二.Toast的常用方法
int | getDuration() 返回Toast视图显示持续的时间. |
int | getGravity() 取得提示信息在屏幕上显示的位置. |
float | getHorizontalMargin() 返回横向栏外空白 |
float | getVerticalMargin() 返回纵向栏外空白. |
View | getView() 返回 View 对象. |
int | getXOffset() 返回相对于参照位置的横向偏移像素量。 |
int | getYOffset() 返回相对于参照位置的纵向偏移像素量 |
staticToast | makeText(Context context, int resId, int duration) 生成一个从资源中取得的包含文本视图的标准 Toast 对象。 context 使用的上下文。通常是你的 Application 或 Activity 对象 resId 要使用的字符串资源ID,可以是已格式化文本。 duration 该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LONG |
staticToast | makeText(Context context, CharSequence text, int duration) 生成一个包含文本视图的标准 Toast 对象. |
void | setDuration(int duration) 设置Toast视图显示持续的时间,LENGTH_LONG表示持续时间较长,LENGTH_SHORT表示持续时间较短 |
void | setGravity(int gravity, int xOffset, int yOffset) 设置提示信息在屏幕上的显示位置. (自定义Toast的显示位置,例如toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。Toast提示的位置xOffset:大于0向右移,小于0向左移) |
void | setMargin(float horizontalMargin, float verticalMargin) 设置视图的栏外空白. horizontalMargin 容器的边缘与提示信息的横向空白(与容器宽度的比) verticalMargin 容器的边缘与提示信息的纵向空白(与容器高度的比)。 |
void | setText(int resId) 更新之前通过 makeText() 方法生成的 Toast 对象的文本内容. resId 为 Toast 指定的新的字符串资源ID。 |
void | setText(CharSequence s) 更新之前通过 makeText() 方法生成的 Toast 对象的文本内容. s 为 Toast 指定的新的文本 |
void | setView(View view) 设置要显示的 View. 注意这个方法可以显示自定义的toast视图,可以包含图像,文字等等。是比较常用的方法 |
void | show() 按照指定的存续期间显示提示信息 |
效果图(有五种不同的Toast显示样式):
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn_1" android:text="@string/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_2" android:text="@string/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_3" android:text="@string/btn3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_4" android:text="@string/btn4" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_5" android:text="@string/btn5" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
toast.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" android:background="#708090" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="带图片文字的Toast" /> </LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello Toast!</string> <string name="app_name">ToastDemo</string> <string name="btn1">系统默认的Toast</string> <string name="btn2">自定义位置的Toast</string> <string name="btn3">带只有图片的Toast</string> <string name="btn4">有图有文字的Toast</string> <string name="btn5">自定义布局的Toast</string> </resources>
ToastDemoActivity.java
package com.android.toast.activity; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; 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 ToastDemoActivity extends Activity { private Button btn_1, btn_2, btn_3, btn_4, btn_5; private Toast toast = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_1.setOnClickListener(new ButtonClick()); btn_2.setOnClickListener(new ButtonClick()); btn_3.setOnClickListener(new ButtonClick()); btn_4.setOnClickListener(new ButtonClick()); btn_5.setOnClickListener(new ButtonClick()); } class ButtonClick implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_1: toast.makeText(ToastDemoActivity.this, "默认的Toast显示", Toast.LENGTH_LONG).show(); break; case R.id.btn_2: // getApplicationContext()得到程序当前的默认Context toast = Toast.makeText(getApplicationContext(), "自定义位置的Toast显示", Toast.LENGTH_LONG); //设置Toast的位置 toast.setGravity(Gravity.CENTER, toast.getXOffset()/2, toast.getYOffset()/2); toast.show(); break; case R.id.btn_3: toast = Toast.makeText(getApplicationContext(), "只有图片的Toast显示", Toast.LENGTH_LONG); ImageView img = new ImageView(ToastDemoActivity.this); img.setImageResource(R.drawable.android); toast.setView(img); toast.show(); break; case R.id.btn_4: toast = Toast.makeText(getApplicationContext(), "有图有字的Toast", Toast.LENGTH_LONG); LinearLayout layout = (LinearLayout)toast.getView(); ImageView img1 = new ImageView(getApplicationContext()); img1.setImageResource(R.drawable.android); layout.addView(img1,0); toast.show(); break; case R.id.btn_5: //将一个xml布局转换成一个view对象 LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.toast,null); Toast toast = new Toast(getApplicationContext()); //在view中查找查找ImageView控件 ImageView image = (ImageView) view.findViewById(R.id.img); image.setImageResource(R.drawable.android); toast.setView(view); toast.show(); break; default: break; } } } }
相关文章推荐
- Android开发学习笔记--计时器的应用实例
- Kotlin开发Android应用实例详解
- android应用开发学习纪实(6)
- 精品Android讲堂--“面向移动终端的应用开发前景及android开发基础实例”[
- Android应用开发学习—Toast使用方法大全
- Android应用开发学习笔记之事件处理
- Asp.net MVC 3实例学习之ExtShop(一)————创建应用并设置开发环境
- Android高级应用开发(基础篇) - stage1 - 学习笔记
- Android开发学习系列-----构建你的第一个应用(2)
- 一步一步的学习android应用开发到系统底层开发之android开发层次及所需技术
- Android应用开发学习笔记之图片切换器
- Android学习探索之Java 8 在Android 开发中的应用
- Android游戏与应用开发最佳学习路线图
- Android应用开发学习笔记之表格布局
- Android应用开发学习笔记之ContentProvider
- Android应用开发学习—Toast使用方法
- Android开发学习笔记(10):NDK安装手顺及应用
- android应用开发常用的一些协议学习
- Android应用开发学习总结(一)——环境搭建与基础准备
- Android应用开发学习笔记之多线程与Handler消息处理机制