展会信息港展会大全

Android中的Toast
来源:互联网   发布日期:2015-10-03 11:20:24   浏览:2202次  

导读:简介Toast是一个弹出Message,允许你便捷地通知用户一些时间,比如:将数据保存到SD卡。值得注意的是用户不能取消Toast。大多数情况下,Toast仅仅是一个简短的messa...

简介

Toast是一个弹出Message,允许你便捷地通知用户一些时间,比如:将数据保存到SD卡。值得注意的是用户不能取消Toast。大多数情况下,Toast仅仅是一个简短的message,但你也可以定制Toast的界面。

创建标准Toast

标准Toast可以通过Toast的静态方法makeText来创建:

Toast.makeText(getApplicationContext(), "Hello, The Code Project!",

Toast.LENGTH_SHORT).show();

Toast.makeText(getApplicationContext(), "Hello, The Code Project!",

Toast.LENGTH_SHORT).show();参数分别为应用上下文,显示的message内容,显示的延迟。你也可以通过R来调用资源文件的内容,如R.string.hello_codeproject。Message显示的延迟可以是LENGTH_SHORT或LENGTH_LONG,默认情况下是LENGTH_SHORT。你也可以通过调用setDuration方法设置延迟。

设置Toast的位置

你可以设置Toast在屏幕上的位置,通过调用如下方法:

Toast toast = Toast.makeText(getApplicationContext(),

"Hello, The Code Project!", Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

Toast toast = Toast.makeText(getApplicationContext(),

"Hello, The Code Project!", Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show(); 其中第一个参数设置位置,第二个参数定义了相对于第一个参数位置的偏移像素。

在标准Toast中添加图像

你需要创建ImageView对象,并调用setImageResource方法,在Toast中添加图像。

Toast toast = Toast.makeText(getApplicationContext(),

"Hello, The Code Project!", Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

LinearLayout toastView = (LinearLayout) toast.getView();

ImageView imageCodeProject = new ImageView(getApplicationContext());

imageCodeProject.setImageResource(R.drawable.codeprojectlogo);

toastView.addView(imageCodeProject, 0);

toast.show();

Toast toast = Toast.makeText(getApplicationContext(),

"Hello, The Code Project!", Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

LinearLayout toastView = (LinearLayout) toast.getView();

ImageView imageCodeProject = new ImageView(getApplicationContext());

imageCodeProject.setImageResource(R.drawable.codeprojectlogo);

toastView.addView(imageCodeProject, 0);

toast.show();

效果如图:

创建定制布局的Toast

首先要创建定制布局的Toast的layout:

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

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

我们创建的这个Toast,有一个header,一个图像和一段message。现在我们需要把layout应用在我们创建的Toast上:

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.codeprojectlogo);

TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);

title.setText("Attention");

TextView text = (TextView) layout.findViewById(R.id.tvTextToast);

text.setText("Hello, The Code Project!");

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

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.codeprojectlogo);

TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);

title.setText("Attention");

TextView text = (TextView) layout.findViewById(R.id.tvTextToast);

text.setText("Hello, The Code Project!");

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

摘自 xinem的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港