`
thunder_yan
  • 浏览: 111559 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android 自定义Toast显示图片

阅读更多

 

Toast用于向用户显示一些帮助/提示。下面展示了5种效果,来说明Toast的强大

注意:

 

LENGTH_LONG---长时间显示视图或文本提示

LENGTH_SHORT---短时间显示视图或文本提示

setGravity(int gravity,int xOffset,int yOffset)---设置提示应该在屏幕上的显示的位置

setDuration(int duartion)---设置提示显示的持续时间

 

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.其他线程

完整代码

Main.java

 

 

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

 

<?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

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" android:layout_width="wrap_content"
 android:background="#ffffffff" android:orientation="vertical"
 android:id="@+id/llToast" >
 <TextView
  android:layout_height="wrap_content"
  android:layout_margin="1dip"
  android:textColor="#ffffffff"
  android:layout_width="fill_parent"
  android:gravity="center"
  android:background="#bb000000"
  android:id="@+id/tvTitleToast" />
 <LinearLayout
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:id="@+id/llToastContent"
  android:layout_marginLeft="1dip"
  android:layout_marginRight="1dip"
  android:layout_marginBottom="1dip"
  android:layout_width="wrap_content"
  android:padding="15dip"
  android:background="#44000000" >
  <ImageView
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_width="wrap_content"
   android:id="@+id/tvImageToast" />
  <TextView
   android:layout_height="wrap_content"
   android:paddingRight="10dip"
   android:paddingLeft="10dip"
   android:layout_width="wrap_content"
   android:gravity="center"
   android:textColor="#ff000000"
   android:id="@+id/tvTextToast" />
 </LinearLayout>
</LinearLayout>
分享到:
评论
2 楼 119568242 2012-07-31  
=-=没看到handler
1 楼 119568242 2012-07-31  
=-=我怎么记得好像Toast只能主线程调用?= =?其他线程会报错- -

相关推荐

    android自定义Toast设定显示时间

    Android 自定义 Toast 设定显示时间是指在 Android 应用程序中,自定义 Toast 的显示时间,而不是使用系统默认的 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG。本文将详细介绍如何使用 WindowManager 实现自定义 Toast...

    android开发之自定义Toast

    一个简单的自定义Toast资源,您可以根据自己的需要更改我的代码即可实现您想要的效果(主要是更改xml文件的布局),这是androidstudio的项目,但是不妨碍在eclipse中使用,可以直接在eclipse中新建android项目,然后将...

    自定义Toast,设置Toast显示位置,自定义Toast的复杂布局

    本篇文章将深入探讨如何自定义Toast,设置其显示位置,并构建复杂的布局。 首先,让我们了解自定义Toast的基本步骤: 1. **创建自定义布局文件**: 在res/layout目录下创建一个新的XML布局文件,例如`custom_...

    Android 自定义 Toast 显示时间

    Android 自定义 Toast 显示时间 Android 自定义 Toast 显示时间是 Android 开发中的一个重要知识点。Toast 是 Android 中的一种常见的提示信息,通常用于显示一些短暂的信息。但是,Android 的默认 Toast 显示时间...

    Android 自定义toast显示效果实例.rar

    Android 源码演示5种toast显示效果,一共是以下几种演示:默认的Toast显示、自定义位置的Toast显示、带图片的Toast显示、完全自定义的Toast显示、长时间的Toast显示,Android 自定义5种toast显示效果。有的Toast效果...

    自定义Toast的显示内容和显示位置

    Android中定义了一个Toast对象,用以弹出一个窗口来给予用户帮助和提示,和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用户的焦点,在弹出Toast的时候,依然可以对之前的界面进行操作,我们在“”...

    Android Toast 自定义背景、图片 随心使用

    本篇文章将深入探讨如何在Android中自定义`Toast`的背景和添加图片,以实现更加个性化的用户体验。 首先,我们要了解`Toast`的基本用法。在Android中,我们可以通过`Toast.makeText()`方法来创建一个`Toast`实例,...

    android 自定义修改toast显示时间

    /* 显示toast,自己定义显示长短。 * param1:activity 传入context * param2:word 我们需要显示的toast的内容 * param3:time length long类型,我们传入的时间长度(如500)*/

    Android中自定义Toast.pdf

    本文将详细介绍如何在Android中自定义Toast。 首先,我们来看如何修改Toast的位置。在Android中,可以使用`setGravity()`方法来改变Toast的显示位置。例如,以下代码将Toast的位置设置为屏幕中央: ```java Toast ...

    android 自定义toast

    本文将深入探讨如何在Android中自定义Toast,包括自定义显示时间和样式。 ### 一、自定义显示时间 系统默认的Toast显示时间分为短时间和长时间,但这些预设值可能并不符合特定场景的需求。要自定义显示时间,我们...

    Android中自定义Toast背景颜色及字体颜色,防止Toast多次创建的ToastUtil

    Android中自定义Toast背景颜色及字体颜色,防止Toast多次创建的ToastUtil,详细了解请移步:http://blog.csdn.net/zxc514257857/article/details/68962539

    Android-Android实现Toast自定义样式

    // 实现上述自定义Toast的逻辑 } public static void showToast(Context context, String text, int duration) { // 调用系统的简单Toast } } ``` 通过这个工具类,可以在整个项目中方便地调用自定义`Toast`...

    android自定义dialog和Toast

    3. **显示自定义Toast**:在需要显示Toast的地方,使用`WindowManager`添加这个自定义的View,并设置动画效果和显示时间。 ```java CustomViewToast customToast = new CustomViewToast(context); WindowManager...

    Android-自定义toast提示可设置做了封装

    本资源"Android-自定义toast提示可设置做了封装"就提供了一种自定义Toast的方法,它允许我们根据需要调整提示的样式、位置、持续时间等特性。 首先,让我们了解一下自定义Toast的基本流程。自定义Toast的核心是创建...

    android默认Toast,各种自定义Toast

    默认的Toast在屏幕中央短暂显示文本信息,但有时开发者可能需要更灵活的展示方式,比如改变位置或样式,这就涉及到了自定义Toast。 ### 1. 默认Toast的使用 默认的Toast使用非常简单,只需要调用`Toast.makeText()...

    android自定义时长Toast

    系统默认的Toast显示时间有限,一般分为短时间和长时间两种模式,但有时开发者可能需要更灵活的控制Toast的显示时长,这就涉及到了自定义Toast。在本篇内容中,我们将深入探讨如何在Android中实现自定义时长的Toast...

    Android自定义Toast提示信息工具类

    Android自定义Toast提示信息工具类,可以实现如下功能: 1、最简单Toast显示 2、自定义图标、图标颜色 3、自定义文本颜色 4、自定义背景颜色 5、设置组件水平、垂直显示位置 6、设置边框宽度、边框颜色

    Android-自定义Toast解决系统Toast存在的问题

    在Dovar66-DToast-f1be133这个压缩包中,可能包含了自定义Toast的相关源代码和资源文件,如布局文件、图片资源等。通过查看这些文件,可以进一步了解自定义Toast的具体实现细节。学习和理解这些内容,对于提升...

    android自定义Dialog、Toast

    3. 创建并显示自定义Toast:通过`Toast.makeText()`创建一个Toast对象,然后用`setView()`设置自定义的View,最后调用`show()`。 ```java Toast toast = Toast.makeText(this, "自定义Toast", Toast.LENGTH_SHORT);...

Global site tag (gtag.js) - Google Analytics