MainActivity:
package com.example.toastdemo;
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnDefault =(Button)this.findViewById(R.id.btnDefault);
final Button btnSelfLocation =(Button)this.findViewById(R.id.btnSelfLocation);
final Button btnSelfPic =(Button)this.findViewById(R.id.btnSelfPic);
final Button btnSelfFull =(Button)this.findViewById(R.id.btnSelfFull);
final Button btnSelfOtherThread =(Button)this.findViewById(R.id.btnSelfOtherThread);
OnTouchListener touchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int ea=event.getAction();
// Log.i("TAG", "Touch:"+ea);
float oriX;
float oriY;
switch(ea){
case MotionEvent.ACTION_DOWN:
Button btn = (Button)v;
if(btn == btnDefault)
{
Toast.makeText(getApplicationContext(), "默认Toast样式", Toast.LENGTH_SHORT).show();
}
else if(btn == btnSelfLocation)
{
Toast toast=Toast.makeText(getApplicationContext(), "自定义显示位置的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else if(btn == btnSelfPic)
{
Toast toast=Toast.makeText(getApplicationContext(), "带图片的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout ll = (LinearLayout)toast.getView();//获取toast的引用布局
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(R.drawable.ic_launcher);
ll.addView(iv);//在toast引用布局中添加图片
toast.show();
}
else if(btn == btnSelfFull)
{
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,(ViewGroup)findViewById(R.id.scrollView1));
layout.setBackgroundColor(Color.BLACK);
ImageView iv = (ImageView)layout.findViewById(R.id.imageView1);
iv.setImageResource(R.drawable.ic_launcher);
TextView tvTitle = (TextView)layout.findViewById(R.id.tvTitle);
tvTitle.setText("Attention");
tvTitle.setTextColor(Color.GREEN);
TextView tvContent = (TextView)layout.findViewById(R.id.tvContent);
tvContent.setText("完全自定义Toast");
tvContent.setTextColor(Color.GREEN);
Toast toast=new Toast(getApplicationContext());//.makeText(getApplicationContext(), "完全自定义Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
//toast.setDuration(0);
toast.setView(layout);
toast.show();
// Toast.makeText(getApplicationContext(), "暂未实现!", Toast.LENGTH_SHORT).show();
}
else if(btn == btnSelfOtherThread)
{
//法1:
Runnable runna = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "其他线程的Toast", Toast.LENGTH_SHORT).show();
}
};
Thread thread = new Thread(runna);
thread.run();
//法2:
// new Thread(new Runnable()
// {
// @Override
// public void run() {
// // TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), "其他线程的Toast", Toast.LENGTH_SHORT).show();
// }
// }).run();
}
break;
case MotionEvent.ACTION_UP:
break;
}
/*如果不添加到switch case中,下面的代码会执行两次!!!
Button btn = (Button)v;
if(btn == btnDefault)
{
Toast.makeText(getApplicationContext(), "默认Toast样式", Toast.LENGTH_SHORT).show();
}
else if(btn == btnSelfLocation)
{
Toast toast=Toast.makeText(getApplicationContext(), "自定义显示位置的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else if(btn == btnSelfPic)
{
Toast toast=Toast.makeText(getApplicationContext(), "带图片的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout ll = (LinearLayout)toast.getView();//获取toast的引用布局
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(R.drawable.ic_launcher);
ll.addView(iv);//在toast引用布局中添加图片
toast.show();
}
else if(btn == btnSelfFull)
{
// LayoutInflater inflater = getLayoutInflater();
// View layout = inflater.inflate(R.layout., root);
Toast.makeText(getApplicationContext(), "暂未实现!", Toast.LENGTH_SHORT).show();
}
else if(btn == btnSelfOtherThread)
{
//法1:
// Runnable runna = new Runnable(){
// @Override
// public void run() {
// // TODO Auto-generated method stub
//
// }
// };
//
// Thread thread = new Thread(runna);
// thread.run();
//法2:
new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "其他线程的Toast", Toast.LENGTH_SHORT).show();
}
}).run();
}*/
return false;
}
};
btnDefault.setOnTouchListener(touchListener);
btnSelfLocation.setOnTouchListener(touchListener);
btnSelfPic.setOnTouchListener(touchListener);
btnSelfFull.setOnTouchListener(touchListener);
btnSelfOtherThread.setOnTouchListener(touchListener);
}
@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;
}
}
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:gravity="center_horizontal"
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/btnDefault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="@string/strDefault" />
<Button
android:id="@+id/btnSelfLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnDefault"
android:layout_centerHorizontal="true"
android:text="@string/strSelfLocation" />
<Button
android:id="@+id/btnSelfPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSelfLocation"
android:layout_centerHorizontal="true"
android:text="@string/strSelfPic" />
<Button
android:id="@+id/btnSelfFull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSelfPic"
android:layout_centerHorizontal="true"
android:text="@string/strSelffull" />
<Button
android:id="@+id/btnSelfOtherThread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSelfFull"
android:layout_centerHorizontal="true"
android:text="@string/strSelfOtherThread" />
</RelativeLayout>
custom.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" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</LinearLayout>