自定义Toast也是常有的工具类 做个笔记 下次方便用
案例:

代码部分如下:

import android.content.Context;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.TextView;import android.widget.Toast;import com.yinji.usbserialcomm.R;public class ToastUtils {         private static ToastUtils mToastUtils=null;    private static Context mContext = null;    private ToastUtils(Context context){             mContext = context.getApplicationContext();    }    public synchronized static ToastUtils getInstance(Context context){             if(mToastUtils==null) mToastUtils=new ToastUtils(context);        return mToastUtils;    }    /**     * 展示toast==LENGTH_SHORT     *     * @param msg     */    public void show(String msg) {             show(msg, Toast.LENGTH_SHORT);    }    /**     * 展示toast==LENGTH_LONG     *     * @param msg     */    public void showLong(String msg) {             show(msg, Toast.LENGTH_LONG);    }    private void show(String massage, int show_length) {             //使用布局加载器,将编写的toast_layout布局加载进来        View view = LayoutInflater.from(mContext).inflate(R.layout.toast_layout, null);        //获取TextView        TextView title = (TextView) view.findViewById(R.id.toast_tv);        //设置显示的内容        title.setText(massage);        Toast toast = new Toast(mContext);        //设置Toast要显示的位置,水平居中并在底部,X轴偏移0个单位,Y轴偏移70个单位,        toast.setGravity(Gravity.CENTER, 0, 0);        //设置显示时间        toast.setDuration(show_length);        toast.setView(view);        toast.show();    }}  

布局文件:
toast_layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/toast_shape">    <TextView        android:id="@+id/toast_tv"        android:layout_width="@dimen/sw_100"        android:layout_height="@dimen/sw_40"        android:layout_centerInParent="true"        android:gravity="center"        android:textColor="#ffffff"        android:textSize="@dimen/sp_12" /></RelativeLayout>

toast_shape.xml

<?xml version="1.0" encoding="utf-8"?><shape    xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="#FF009898" />    <corners android:radius="20px"/></shape>

更多相关文章

  1. android window.requestWindowFeature()常用方法
  2. android 中让activity全屏幕显示
  3. android 学习记录
  4. Android(安卓)导航类型
  5. Android之TextSwitcher
  6. [问题记录]Android(安卓)target 27 适配的一个坑
  7. Android(安卓)APN设置之切换网络
  8. android adb 读写模式 挂载文件系统
  9. Android(安卓)ImageView的scaleType & setScaleType

随机推荐

  1. Android进程间通信(一):AIDL使用详解
  2. Android之父深入解析Android
  3. [置顶] android调用第三方库——第四篇—
  4. android 控件位置常用布局
  5. 流水笔记--2015-9-13
  6. Android野史系列:3.利用Android Studio开
  7. Android的简单介绍
  8. Android版本演进中的兼容性问题
  9. Ubuntu16.04下安装Android机顶盒(Android4
  10. 使用Eclipse搭建Android的开发环境