Android 中的Toast是一个弹出的提示消息框,以友好的方式提示用户,例如保存数据成功等。
下面以一个例子来说明Toast的使用以及标准的Toast显示方式和自定义的Toast显示方式来提示友好的信息。以下是该Demo的程序结构图:
[1] res/layout目录下的 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"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/show"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Show Toast"/>
- </LinearLayout>
[2] res/layout目录下的 customtoast.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>
[3] src目录下的 MainActivity.java源码:
- package com.andyidea.demo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- 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 {
- Button btn;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button)findViewById(R.id.show);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //标准方式
- showToast1();
- //标准方式上添加图片
- showToast2();
- //自定义显示方式
- showToast3();
- }
- });
- }
- /**
- * Basic Standard Toast
- * 标准提示信息方式
- */
- private void showToast1(){
- Toast toast = Toast.makeText(getApplicationContext(),"Hello, This is Andy!", Toast.LENGTH_LONG);
- toast.show();
- }
- /**
- * Adding an Image to the Standard Toast
- * 在标准显示方式基础上添加图片
- */
- private void showToast2(){
- Toast toast = Toast.makeText(getApplicationContext(),"Hello, This is Andy!", 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();
- }
- /**
- * Creating a Toast with Custom Layout
- * 创建自定义的提示信息方式
- */
- private void showToast3(){
- LayoutInflater inflater = getLayoutInflater();
- View layout = inflater.inflate(R.layout.customtoast,
- (ViewGroup) findViewById(R.id.llToast));
- ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
- image.setImageResource(R.drawable.page);
- TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
- title.setText("Attention");
- TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
- text.setText("Hello, This is Andy!");
- Toast toast = new Toast(getApplicationContext());
- toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);
- toast.show();
- }
- }
[4] 点击Show Toast按钮查看效果如下:
标准方式
标准方式+图片
自定义显示方式
相关推荐
主要为大家详细介绍了android自定义Toast设定显示时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
android Toast对象的使用 自定义Toast
一个简单的自定义Toast资源,您可以根据自己的需要更改我的代码即可实现您想要的效果(主要是更改xml文件的布局),这是androidstudio的项目,但是不妨碍在eclipse中使用,可以直接在eclipse中新建android项目,然后将...
Android:实现Toast自定义样式(包括:自定义位置、带图片等)、自定义显示时长(包括可用系统时长、可自定义)
Android Toast 自定义背景、图片 随心使用
默认Toast(大家都会),自定义Toast位置,自定义Toast样式
自己整理的自定义控件方法,看了你就会做自定义Dialog和Toast了,没有多余代码,很简洁。背景、按钮都能自定义
android 自定义toast,toast自定义显示时间,toast自定义显示样式
Android中自定义Toast.pdf 学习资料 复习资料 教学资源
自定义Toast,设置Toast显示位置,自定义Toast的复杂布局 博客地址:http://blog.csdn.net/xy_nyle/article/details/20137953
/* 显示toast,自己定义显示长短。 * param1:activity 传入context * param2:word 我们需要显示的toast的内容 * param3:time length long类型,我们传入的时间长度(如500)*/
android利用布局文件自定义Dialog、Toast
Android中自定义Toast背景颜色及字体颜色,防止Toast多次创建的ToastUtil,详细了解请移步:http://blog.csdn.net/zxc514257857/article/details/68962539
android自定义时长Toast,Toast 显示时间可以自己设定
自定义toast提示,可设置,做了封装
html自定义提示框,仿android的Toast功能,可以自定义显示时间!
Android 源码演示5种toast显示效果,一共是以下几种演示:默认的Toast显示、自定义位置的Toast显示、带图片的Toast显示、完全自定义的Toast显示、长时间的Toast显示,Android 自定义5种toast显示效果。有的Toast效果...
pc模拟Android端toast效果,自定义两种形式 详细参照此贴:https://blog.csdn.net/qq_37603131/article/details/124287143?spm=1001.2014.3001.5501
android中Toast和Notification的应用.