首页 > 系统 > Android > 正文

Android中自定义Toast

2019-11-09 17:32:06
字体:
来源:转载
供稿:网友

在Android程序中,Toast可谓用处多多,Toast本身作为消息提示,不占用焦点,用户可以处理其他程序的同时接收Toast中显示的信息。但是我们平常看见的Toast都是黑框白字的,那么我们可以改变原有的Toast,制作我们自定义的Toast吗。这个当然可以。Toast类本身提供了定义Toast布局、显示字体等一些方法。下面以一个例子说明: 新建一个Android工程: activity_main.xml:

<LinearLayout 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:orientation="vertical" android:gravity="center_horizontal" tools:context=".MainActivity" > <Button android:id="@+id/showDefineToastButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示简单的自定义的Toast" /> <Button android:id="@+id/showDefineToastButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示布局自定义Toast" /></LinearLayout>

新建一个布局文件用于自定义Toast的布局: toast_view.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:gravity="center_horizontal" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个自定义布局的Toast"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/></LinearLayout>

简单的布局,就不介绍了,接下来是MainAcitivty.java:

import android.os.Bundle;import android.app.Activity;import android.graphics.Color;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;public class MainActivity extends Activity { PRivate Button button = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.showDefineToastButton1); button.setOnClickListener(listener); button = (Button) findViewById(R.id.showDefineToastButton2); button.setOnClickListener(listener); } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.showDefineToastButton1: showDefineToast1(); break; case R.id.showDefineToastButton2: showDefineToast2(); break; } } }; private void showDefineToast1() { Toast toast = Toast.makeText(this, "这是一个简单的自定义Toast", Toast.LENGTH_SHORT); LinearLayout toastView = (LinearLayout) toast.getView(); toastView.setBackgroundColor(Color.GREEN); toast.show(); } private void showDefineToast2() { LayoutInflater inflater = LayoutInflater.from(this); View toastView = inflater.inflate(R.layout.toast_view, null); // 用布局解析器来解析一个布局去适配Toast的setView方法 Toast toast = new Toast(this); toast.setView(toastView); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } @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; }}

运行程序,单击第一个按钮: 这里写图片描述 单击第二个按钮: 这里写图片描述 完成,我们成功自定义了我们自己的Toast。

在这里需要注意的是当你使用你自己自定义布局去代替原有Toast布局之后,或者在你用Toast的构造方法构造出一个新的Toast对象的时候,你是不能使用Toast.setText(SequenceChar );方法的,否则程序会崩溃,产生空指针异常。前者是因为Toast.setText();方法不能改变自定义布局文件中的内容,后者是因为刚刚新建出来的Toast对象还没有设置布局,自然不能添加信息。

如果博客中有什么不对的地方还请多多指点。 谢谢观看。。。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表