Toast 的效果如下:
MainActivity.java :
package com.android.mytoasttest;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private static final String CONTENT_1 = "默认的Toast显示";
private static final String CONTENT_2 = "自定义位置的Toast显示";
private static final String CONTENT_3 = "带图片的Toast显示";
private Button button1, button2, button3, button4;
private Toast toast = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
clickFromButton1();
break;
case R.id.button2:
clickFromButton2();
break;
case R.id.button3:
// clickFromButton3One();
// 第二中方法
clickFromButton3Two();
break;
case R.id.button4:
clickFromButton4();
break;
}
}
private void clickFromButton1(){
toast = Toast.makeText(this, CONTENT_1, Toast.LENGTH_LONG);
toast.show();
}
private void clickFromButton2(){
toast = Toast.makeText(this, CONTENT_2, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
private void clickFromButton3One(){
toast = Toast.makeText(this, CONTENT_3, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 50, -100);
LinearLayout layout = (LinearLayout) toast.getView();
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.tools);
layout.addView(image, 0);
toast.show();
}
private void clickFromButton3Two(){
toast = new Toast(this);
toast.setGravity(Gravity.CENTER, 50, -100);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.tools);
LinearLayout ll = new LinearLayout(this);
ll.addView(image);
TextView textView = new TextView(this);
textView.setText(CONTENT_3);
textView.setTextSize(24);
textView.setTextColor(Color.RED);
ll.addView(textView);
// 设置 Toast 显示自定义 View
toast.setView(ll);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
private void clickFromButton4(){
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.item,
(ViewGroup) findViewById(R.id.item_layout));
TextView txtView_Title = (TextView) view
.findViewById(R.id.txt_Title);
ImageView imageView = (ImageView) view
.findViewById(R.id.image_toast);
TextView txtView_Context = (TextView) view
.findViewById(R.id.txt_context);
toast = new Toast(this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
}
}
activity_main.xml :
<LinearLayout 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:orientation="vertical"
tools:context="com.android.mytoasttest.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认的Toast显示"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义位置的Toast显示"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带图片的Toast显示"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义的Toast显示"/>
</LinearLayout>
item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:background="#666666"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="250dp">
<TextView
android:id="@+id/txt_Title"
android:layout_gravity="center|top"
android:text="自定义Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#aabbccdd">
<ImageView
android:id="@+id/image_toast"
android:src="@drawable/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt_context"
android:textColor="#ffffffff"
android:layout_gravity="center|right"
android:text="自定义Toast,左边的是一幅tool工具"
android:layout_marginLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>