一.Toast的简介
Toast是Android中一种提供给用户简短信息的视图,该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能已不显眼的方式,使用户看到你提供的信息。显示的时间是有限制的,过一段时间后会自动消失,不过Toast本身可以控制显示时间的长短。
二.Toast的常用方法
int
|
getDuration()
返回Toast视图显示持续的时间.
|
int
|
getGravity()
取得提示信息在屏幕上显示的位置.
|
float
|
getHorizontalMargin()
返回横向栏外空白
|
float
|
getVerticalMargin()
返回纵向栏外空白.
|
View
|
getView()
返回 View 对象.
|
int
|
getXOffset()
返回相对于参照位置的横向偏移像素量。
|
int
|
getYOffset()
返回相对于参照位置的纵向偏移像素量
|
static Toast
|
makeText(Context context, int resId, int duration)
生成一个从资源中取得的包含文本视图的标准 Toast 对象。
context 使用的上下文。通常是你的 Application 或 Activity 对象
resId 要使用的字符串资源ID,可以是已格式化文本。
duration 该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LONG
|
static Toast
|
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的不同显示样式
效果图(有五种不同的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;
- }
- }
- }
- }
-
本文转自 lingdududu 51CTO博客,原文链接:
http://blog.51cto.com/liangruijun/638913