Android中的Toast用于向用户显示一些帮助/提示。
以下是关于Toast的一些使用的总结,希望对大家的学习和解决问题提供一些帮助:
实现效果图:
1、先贴出布局文件:
(1)activity_main.xml:
这里定义了四个button
<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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<Button
android:id="@+id/toast_btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示默认Toast" />
<Button
android:id="@+id/toast_btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="改变位置的Toast" />
<Button
android:id="@+id/toast_btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示图片的Toast" />
<Button
android:id="@+id/toast_btn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="完全自定义的Toast" />
</LinearLayout>
</RelativeLayout>
(2)toast_layout.xml:
这是个自定义的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="30dip"
android:gravity="center"
android:text="这个是自定义的Toast!" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/topimg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="内容部分,我们可以随便写!" />
</LinearLayout>
java代码是四个Toast的具体实现的功能
package com.imooc.toast;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initEvent();
}
/**
* 初始化点击事件
*/
private void initEvent() {
findViewById(R.id.toast_btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showToast1();
}
});
findViewById(R.id.toast_btn2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showToast2();
}
});
findViewById(R.id.toast_btn3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showToast3();
}
});
findViewById(R.id.toast_btn4).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showToast4();
}
});
}
/**
* 显示默认toast
*/
private void showToast1() {
// Toast toast =
// Toast.makeText(this,"这是一个默认的Toast!",Toast.LENGTH_SHORT);
Toast toast = Toast
.makeText(this, R.string.app_name, Toast.LENGTH_LONG);
toast.show();
}
/**
* 显示自定义位置的Toast
*/
private void showToast2() {
Toast toast = Toast.makeText(this, "改变位置的Toast!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
/* 第二个参数为正是向右偏移,为负是向左偏移;第三个参数为正是向下偏移 ,为负是向上偏移 */
toast.show();
}
/**
* 带有图片的toast;
*/
private void showToast3() {
Toast toast = Toast.makeText(this, "带有图片的Toast!", Toast.LENGTH_LONG);
LinearLayout toast_layout = (LinearLayout) toast.getView();/* 这句是找到图片 */
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.topimg);
toast_layout.addView(iv, 0);/* 添加图片,定义为0是图片在上,定义为1是图片在下 */
toast.show();
}
/**
* 完全自定义的Toast
*/
private void showToast4() {
LayoutInflater inflater = LayoutInflater.from(this);
View toast_view = inflater.inflate(R.layout.toast_layout, null);/* 加载布局 */
Toast toast = new Toast(this);
toast.setView(toast_view);/* 将布局添加进去 */
toast.show();
}
}
3、总结一下Toast的常用的用法:
(1)Toast.makeText(context,text,duration);//返回值为Toast
(2)toast.setDuration(duration);//设置持续时间
(3)toast.setGravity(gravity,xOffsert,yOffset);//设置toast位置
(4)toast.setText(s);//设置提示内容
(5)toast.show();//显示