前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android中自定义Toast

Android中自定义Toast

作者头像
指点
发布2019-01-18 17:35:59
9320
发布2019-01-18 17:35:59
举报
文章被收录于专栏:指点的专栏指点的专栏

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

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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对象还没有设置布局,自然不能添加信息。

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年02月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档