Toast的自定义用法

Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

1.默认效果

代码

Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();

2.自定义显示位置效果

代码

toast = Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

3.带图片效果

代码

toast = Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();

4.完全自定义效果

代码

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

5.其他线程

代码

new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();

完整代码

1.Main,java

  1. package com.wjq.toast;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.Gravity;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.View.OnClickListener;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. public class Main extends Activity implements OnClickListener {
  15. Handler handler = new Handler();
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. findViewById(R.id.btnSimpleToast).setOnClickListener(this);
  21. findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
  22. this);
  23. findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
  24. findViewById(R.id.btnCustomToast).setOnClickListener(this);
  25. findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
  26. }
  27. public void showToast() {
  28. handler.post(new Runnable() {
  29. @Override
  30. public void run() {
  31. Toast.makeText(getApplicationContext(), "我来自其他线程!",
  32. Toast.LENGTH_SHORT).show();
  33. }
  34. });
  35. }
  36. @Override
  37. public void onClick(View v) {
  38. Toast toast = null;
  39. switch (v.getId()) {
  40. case R.id.btnSimpleToast:
  41. Toast.makeText(getApplicationContext(), "默认Toast样式",
  42. Toast.LENGTH_SHORT).show();
  43. break;
  44. case R.id.btnSimpleToastWithCustomPosition:
  45. toast = Toast.makeText(getApplicationContext(),
  46. "自定义位置Toast", Toast.LENGTH_LONG);
  47. toast.setGravity(Gravity.CENTER, 0, 0);
  48. toast.show();
  49. break;
  50. case R.id.btnSimpleToastWithImage:
  51. toast = Toast.makeText(getApplicationContext(),
  52. "带图片的Toast", Toast.LENGTH_LONG);
  53. toast.setGravity(Gravity.CENTER, 0, 0);
  54. LinearLayout toastView = (LinearLayout) toast.getView();
  55. ImageView imageCodeProject = new ImageView(getApplicationContext());
  56. imageCodeProject.setImageResource(R.drawable.icon);
  57. toastView.addView(imageCodeProject, 0);
  58. toast.show();
  59. break;
  60. case R.id.btnCustomToast:
  61. LayoutInflater inflater = getLayoutInflater();
  62. View layout = inflater.inflate(R.layout.custom,
  63. (ViewGroup) findViewById(R.id.llToast));
  64. ImageView image = (ImageView) layout
  65. .findViewById(R.id.tvImageToast);
  66. image.setImageResource(R.drawable.icon);
  67. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
  68. title.setText("Attention");
  69. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
  70. text.setText("完全自定义Toast");
  71. toast = new Toast(getApplicationContext());
  72. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
  73. toast.setDuration(Toast.LENGTH_LONG);
  74. toast.setView(layout);
  75. toast.show();
  76. break;
  77. case R.id.btnRunToastFromOtherThread:
  78. new Thread(new Runnable() {
  79. public void run() {
  80. showToast();
  81. }
  82. }).start();
  83. break;
  84. }
  85. }
  86. }
package com.wjq.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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
 Handler handler = new Handler();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  findViewById(R.id.btnSimpleToast).setOnClickListener(this);
  findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
    this);
  findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
  findViewById(R.id.btnCustomToast).setOnClickListener(this);
  findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);

 }

 public void showToast() {
  handler.post(new Runnable() {

   @Override
   public void run() {
    Toast.makeText(getApplicationContext(), "我来自其他线程!",
      Toast.LENGTH_SHORT).show();

   }
  });
 }

 @Override
 public void onClick(View v) {
  Toast toast = null;
  switch (v.getId()) {
  case R.id.btnSimpleToast:
   Toast.makeText(getApplicationContext(), "默认Toast样式",
     Toast.LENGTH_SHORT).show();
   break;
  case R.id.btnSimpleToastWithCustomPosition:
   toast = Toast.makeText(getApplicationContext(),
     "自定义位置Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   toast.show();
   break;
  case R.id.btnSimpleToastWithImage:
   toast = Toast.makeText(getApplicationContext(),
     "带图片的Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   LinearLayout toastView = (LinearLayout) toast.getView();
   ImageView imageCodeProject = new ImageView(getApplicationContext());
   imageCodeProject.setImageResource(R.drawable.icon);
   toastView.addView(imageCodeProject, 0);
   toast.show();
   break;
  case R.id.btnCustomToast:
   LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom,
     (ViewGroup) findViewById(R.id.llToast));
   ImageView image = (ImageView) layout
     .findViewById(R.id.tvImageToast);
   image.setImageResource(R.drawable.icon);
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");
   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
   text.setText("完全自定义Toast");
   toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
   break;
  case R.id.btnRunToastFromOtherThread:
   new Thread(new Runnable() {
    public void run() {
     showToast();
    }
   }).start();
   break;

  }

 }
}


2.main,xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
  5. <Button android:layout_height="wrap_content"
  6. android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
  7. android:text="默认"></Button>
  8. <Button android:layout_height="wrap_content"
  9. android:layout_width="fill_parent" android:text="自定义显示位置"
  10. android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
  11. <Button android:layout_height="wrap_content"
  12. android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
  13. android:text="带图片"></Button>
  14. <Button android:layout_height="wrap_content"
  15. android:layout_width="fill_parent" android:text="完全自定义"
  16. android:id="@+id/btnCustomToast"></Button>
  17. <Button android:layout_height="wrap_content"
  18. android:layout_width="fill_parent" android:text="其他线程"
  19. android:id="@+id/btnRunToastFromOtherThread"></Button>
  20. </LinearLayout>
<?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" android:padding="5dip" android:gravity="center">
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
  android:text="默认"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="自定义显示位置"
  android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
  android:text="带图片"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="完全自定义"
  android:id="@+id/btnCustomToast"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="其他线程"
  android:id="@+id/btnRunToastFromOtherThread"></Button>

</LinearLayout>


3.custom.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_height="wrap_content" android:layout_width="wrap_content"
  5. android:background="#ffffffff" android:orientation="vertical"
  6. android:id="@+id/llToast" >
  7. <TextView
  8. android:layout_height="wrap_content"
  9. android:layout_margin="1dip"
  10. android:textColor="#ffffffff"
  11. android:layout_width="fill_parent"
  12. android:gravity="center"
  13. android:background="#bb000000"
  14. android:id="@+id/tvTitleToast" />
  15. <LinearLayout
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical"
  18. android:id="@+id/llToastContent"
  19. android:layout_marginLeft="1dip"
  20. android:layout_marginRight="1dip"
  21. android:layout_marginBottom="1dip"
  22. android:layout_width="wrap_content"
  23. android:padding="15dip"
  24. android:background="#44000000" >
  25. <ImageView
  26. android:layout_height="wrap_content"
  27. android:layout_gravity="center"
  28. android:layout_width="wrap_content"
  29. android:id="@+id/tvImageToast" />
  30. <TextView
  31. android:layout_height="wrap_content"
  32. android:paddingRight="10dip"
  33. android:paddingLeft="10dip"
  34. android:layout_width="wrap_content"
  35. android:gravity="center"
  36. android:textColor="#ff000000"
  37. android:id="@+id/tvTextToast" />
  38. </LinearLayout>
  39. </LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值