Toast的自定义

写完dialog的自定义现在开始写写toast了,其实对于toast的讲解网上也有很多,并且很容易。先说说为什么要写Toast的自定义吧,对于一个页面的Toast的点击,不断的让它弹出Toast,会导致app的卡顿,还有当你退出app的时候它还在那弹出toast,并不是所有人都向开发者一样那么规规矩矩的操作,会有各种一些列想不到的操作,就比如狂点。还有一点就是Android端和IOS端的统一,不知道你们 有没有,我们公司老是觉得ios好看些,什么东西就要跟IOS保持一致,甚至连Toast也要,ios的toast是蓝绿蓝绿的,还在也不是在屏幕下方,对此我将我app里面的toast全部给改了。

老规矩,上代码:

 private View toastRoot;
    private Toast toast = null;//全局定义

    private void showToast(String msg) {//toast的显示方法,
        TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
        if (toast == null) {
            toast = new Toast(ActorListActivity.this);
            tv.setText(msg);
        } else {
            tv.setText(msg);
        }
        toast.setView(toastRoot);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
不要忘了
toastRoot = getLayoutInflater().inflate(R.layout.activity_sign, null);//activity启动时候调用。
接下来就是对于显示toast的调用了,传入想toast的字符串即可。

最后还是将布局文件粘贴出来吧,我是一个敲代码中比较懒的,能copy就不会手写,这也是为了保证效率和正确率。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/greenradius"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/TextViewInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#fff"
        android:textSize="16sp"/>



</LinearLayout>

布局的样式,字体背景颜色自己调整。

猜你喜欢

转载自blog.csdn.net/greatdaocaoren/article/details/54861325